I have trouble with the Microsoft.Azure.ServiceBus.Message
class. I want to create a Message object containing a payload object and then read this object back from it. In my current example I am not even sending the Message through a real Azure bus; I'm just creating it in memory and then trying to read it.
I cannot figure out what type I am supposed to read the message body as. I've tried byte[]
, string
and the original object type. In all my cases I get an XmlException
: "The input source is not correctly formatted".
Can someone please tell me what I am doing wrong, either when encoding or decoding the Message?
[DataContract]
public class Thingy
{
[DataMember]
public string Doodad { get; set; }
}
private static Message CreateMessage()
{
var entityMessage = new Thingy {Doodad = "foobar"};
var serializedMessageBody = JsonConvert.SerializeObject(entityMessage);
var contentType = typeof(Thingy).AssemblyQualifiedName;
var bytes = Encoding.UTF8.GetBytes(serializedMessageBody);
var message = new Message(bytes) {ContentType = contentType};
return message;
}
[Test]
public void ReadMessageBytes()
{
var message = CreateMessage();
var body = message.GetBody<byte[]>();
Console.WriteLine(body);
}
[Test]
public void ReadMessageString()
{
var message = CreateMessage();
var body = message.GetBody<string>();
Console.WriteLine(body);
}
[Test]
public void ReadMessageThingy()
{
var message = CreateMessage();
var body = message.GetBody<Thingy>();
Console.WriteLine(body);
}