title: Service bus System.InvalidOperationException
body: <p>I am trying to send a message to the azure service bus using datacontractjson serializer. I get the below error message very often</p>
  
  <p>Unhandled Exception: System.InvalidOperationException: Failed to serialize the message because its body stream has been partially consumed.</p>
  
  <p>Here is my code</p>
  
  <pre><code>[DataContract]
  class flow
  {
      [DataMember]
      public string srcip {get; set;}
      [DataMember]
      public string dstip {get; set;}
  }
  </code></pre>
  
  <p>Main code:</p>
  
  <pre><code>class Program
     {
          static void Main(string[] args)
         {
          string connectionString = "XXXXXX";
  
          MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);
  
          flow f = new flow()
          {
              srcip = "10.1.1.1",
              dstip = "20.1.1.1"
          };
  
  
          //Sending a message
  
          MessageSender testQueueSender = factory.CreateMessageSender("XXXXX");
  
          MemoryStream stream1 = new MemoryStream();
          DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(flow));
          ser.WriteObject(stream1, f);
  
          BrokeredMessage message = new BrokeredMessage(stream1);
  
          testQueueSender.Send(message);
  
          Console.WriteLine("Message(s) sent.");
  
  
  
  
  
          Console.WriteLine("Done, press a key to continue...");
  
          Console.ReadKey();
      }
  }
  </code></pre>
  
  <p>}</p>