title: Encoding of BrokeredMessage Body in Azure Service Bus
body: <p>We have the below wrapper around the Azure Service Bus Client:</p>
  
  <pre><code>public virtual void SendMessage(object bodyObject)
  {
      var brokeredMessage = 
          _messageBodySerializer.SerializeMessageBody(bodyObject);
  
      _queueClient.Send(brokeredMessage);
  }
  </code></pre>
  
  <p>With the <code>SerializeMessageBody</code> consisting of:</p>
  
  <pre><code>public BrokeredMessage SerializeMessageBody(object bodyObject)
  {
      var brokeredMessage = new BrokeredMessage(bodyObject);
  
      brokeredMessage.Properties[MESSAGE_TYPE_PROPERTY_KEY] = 
                      bodyObject.GetType().AssemblyQualifiedName;
  
      return brokeredMessage;
  }
  </code></pre>
  
  <p>I've written an integration test to check this works by running <code>SendMessage</code> then viewing the message contents using <a href="http://blogs.msdn.com/b/paolos/archive/2013/04/12/service-bus-explorer-2-0-released.aspx" rel="noreferrer">Service Bus Explorer 2.0</a>.  We are doing this to ensure we can repair and resend messages using the tool to modify the XML.  The message I am sending looks like this:</p>
  
  <pre><code>[DataContract]
  public class TestServiceBusMessage
  {
      [DataMember]
      public Guid ExternalIdentifier { get; set; }
  
      [DataMember]
      public int Identifier { get; set; }
  
      [DataMember]
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>The problem is; when the message body is viewed on the tool, instead of looking like XML it instead comes out like below:</p>
  
  <pre><code>@TestServiceBusMessageWhttp://schemas.datacontract.org/2004/07/IntegrationTests.Common.Azure.ServiceBus   i)http://www.w3.org/2001/XMLSchema-instance@ExternalIdentifier?$13d81b90-d932-437f-a11c-9106412b6c4a@
  Identifier?&amp;    @Name?Test Message   
  </code></pre>
  
  <p>I assume this is an encoding issue; however I cannot see a way to set the encoding when passing the body into the <code>BrokeredMessage</code> constructor; or defining it through a <code>DataContract</code> attribute.</p>
  
  <p>How would I get around this encoding issue?  Would I need to provide my own <a href="http://msdn.microsoft.com/en-us/library/windowsazure/hh330723.aspx" rel="noreferrer">serializer</a> / <a href="http://msdn.microsoft.com/en-us/library/windowsazure/hh330764.aspx" rel="noreferrer">stream</a> or is there a way to force the default serializer to encode correctly?</p>