Udostępnij za pośrednictwem


MessageInteropExtensions Class

Definition

A Message Extension Class that provides extension methods to deserialize the body of a message that was serialized and sent to ServiceBus Queue/Topic using the WindowsAzure.Messaging client library. The WindowsAzure.Messaging client library serializes objects using the Microsoft.Azure.ServiceBus.InteropExtensions.DataContractBinarySerializer (default serializer) or DataContractSerializer when sending message. This class provides extension methods to deserialize and retrieve the body of such messages.

public static class MessageInteropExtensions
type MessageInteropExtensions = class
Public Module MessageInteropExtensions
Inheritance
MessageInteropExtensions

Remarks

1. If a message is only being sent and received using this Microsoft.Azure.ServiceBus client library, then the below extension methods are not relevant and should not be used.

2. If this client library will be used to receive messages that were sent using both WindowsAzure.Messaging client library and this (Microsoft.Azure.ServiceBus) library, then the Users need to add a User property UserProperties while sending the message. On receiving the message, this property can be examined to determine if the message was from WindowsAzure.Messaging client library and if so use the message.GetBody() extension method to get the actual body associated with the message.

---------------------------------------------- Scenarios to use the GetBody Extension method: ---------------------------------------------- If message was constructed using the WindowsAzure.Messaging client library as follows:

var message1 = new BrokeredMessage("contoso"); // Sending a plain string
var message2 = new BrokeredMessage(sampleObject); // Sending an actual customer object
var message3 = new BrokeredMessage(Encoding.UTF8.GetBytes("contoso")); // Sending a UTF8 encoded byte array object

await messageSender.SendAsync(message1);
await messageSender.SendAsync(message2);
await messageSender.SendAsync(message3);

Then retrieve the original objects using this client library as follows: (By default Microsoft.Azure.ServiceBus.InteropExtensions.DataContractBinarySerializer will be used to deserialize and retrieve the body. If a serializer other than that was used, pass in the serializer explicitly.)

var message1 = await messageReceiver.ReceiveAsync();
var returnedData1 = message1.GetBody<string>();

var message2 = await messageReceiver.ReceiveAsync();
var returnedData2 = message1.GetBody<SampleObject>();

var message3 = await messageReceiver.ReceiveAsync();
var returnedData3Bytes = message1.GetBody<byte[]>();
Console.WriteLine($"Message3 String: {Encoding.UTF8.GetString(returnedData3Bytes)}");

------------------------------------------------- Scenarios to NOT use the GetBody Extension method: ------------------------------------------------- If message was sent using the WindowsAzure.Messaging client library as follows: var message4 = new BrokeredMessage(new MemoryStream(Encoding.UTF8.GetBytes("contoso"))); await messageSender.SendAsync(message4);

Then retrieve the original objects using this client library as follows: var message4 = await messageReceiver.ReceiveAsync(); string returned = Encoding.UTF8.GetString(message4.Body); // Since message was sent as Stream, no deserialization required here.

Methods

GetBody<T>(Message, XmlObjectSerializer)

Deserializes the body of a message that was serialized using XmlObjectSerializer

Applies to