Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BrokeredMessage Deseralization comes with unwanted String

Passing a brokered message over ServiceBus with out any custom DataContractSerializer[as Default XML Serializer Take Place].

var message = new BrokeredMessage(objMess.MessageBody);

Note: Mainly the message body is type of HTML Emails.

But When on the message delivered to worker role after deserialization , i see some random text is appended in top body,

var reader = new StreamReader(receivedMessage.GetBody<Stream>());

@string3http://schemas.microsoft.com/2003/10/Serialization/�  .
Rest of Message Body 

i tried to give custom DataContractSerializer. but that messed up with HTML symbols.

Some Formatting the content for Service Bus messages article i found but still finding a way to get rid of schema String.

As per now i doing substring with message body.

like image 357
joshua Avatar asked Dec 16 '22 04:12

joshua


2 Answers

Seems like you have mismatched sender and receiver types. ServiceBus BrokeredMessages should be used like this:

1) If you send with var message = new BrokeredMessage(object) you should receive with receivedMessage.GetBody<typeof(object)>()

2) If you send with var message = new BrokeredMessage(object, XmlObjectSerializer> -> you should receive with receivedMessage.GetBody<typeof(object)>(XmlObjectSerializer)

3) If you send with var message = new BrokeredMessage(Stream) You should receive with receivedMessage.GetBody<Stream>

This should be transparent to you. Just put the type that you are sending in the Receiver. receivedMessage.GetBody<String>() if it's a string or receivedMessage.GetBody<TypeOfMessageBody>()

like image 79
krolth Avatar answered Jan 18 '23 23:01

krolth


This is just an addition to @krolths answer.

I have a situation where we're getting the message the same way the OP does and ran into this problem for my unit tests. The problem is indeed that we put one thing in and take something else out. We're transferring JSON so it feels natural to put a string in. But in order for the string to be properly serialized by the BrokeredMessage constructor we have to wrap it in a stream. This is how we do that.

private BrokeredMessage GetBrokeredMessageWithBody(object body)
{
    var json = JsonConvert.SerializeObject(body);
    return new BrokeredMessage(GenerateStreamFromString(json));
}

private Stream GenerateStreamFromString(string s)
{
    var stream = new MemoryStream();
    var writer = new StreamWriter(stream);
    writer.Write(s);
    writer.Flush();
    stream.Position = 0;
    return stream;
}
like image 38
Johan Gov Avatar answered Jan 18 '23 22:01

Johan Gov