سؤال

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.

هل كانت مفيدة؟

المحلول

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>()

نصائح أخرى

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;
}

Of what type is your objMess.MessageBody? Make sure that it is the same type you are polling on the receiver side as you are doing a GetBody<Stream>()

The first line is done by Service Bus to serialize your object. You can see this happen when you check your queue with tools like Service Bus Explorer.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top