| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus.InteropExtensions |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.IO; |
| | 8 | | using System.Runtime.Serialization; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// A Message Extension Class that provides extension methods to deserialize |
| | 12 | | /// the body of a message that was serialized and sent to ServiceBus Queue/Topic |
| | 13 | | /// using the WindowsAzure.Messaging client library. The WindowsAzure.Messaging |
| | 14 | | /// client library serializes objects using the |
| | 15 | | /// <see cref="DataContractBinarySerializer"/> (default serializer) or <see cref="DataContractSerializer"/> |
| | 16 | | /// when sending message. This class provides extension methods to deserialize |
| | 17 | | /// and retrieve the body of such messages. |
| | 18 | | /// </summary> |
| | 19 | | /// <remarks> |
| | 20 | | /// 1. If a message is only being sent and received using this Microsoft.Azure.ServiceBus |
| | 21 | | /// client library, then the below extension methods are not relevant and should not be used. |
| | 22 | | /// |
| | 23 | | /// 2. If this client library will be used to receive messages that were sent using both |
| | 24 | | /// WindowsAzure.Messaging client library and this (Microsoft.Azure.ServiceBus) library, |
| | 25 | | /// then the Users need to add a User property <see cref="Message.UserProperties"/> |
| | 26 | | /// while sending the message. On receiving the message, this property can be examined to |
| | 27 | | /// determine if the message was from WindowsAzure.Messaging client library and if so |
| | 28 | | /// use the message.GetBody() extension method to get the actual body associated with the message. |
| | 29 | | /// |
| | 30 | | /// ---------------------------------------------- |
| | 31 | | /// Scenarios to use the GetBody Extension method: |
| | 32 | | /// ---------------------------------------------- |
| | 33 | | /// If message was constructed using the WindowsAzure.Messaging client library as follows: |
| | 34 | | /// <code> |
| | 35 | | /// var message1 = new BrokeredMessage("contoso"); // Sending a plain string |
| | 36 | | /// var message2 = new BrokeredMessage(sampleObject); // Sending an actual customer object |
| | 37 | | /// var message3 = new BrokeredMessage(Encoding.UTF8.GetBytes("contoso")); // Sending a UTF8 encoded byte array |
| | 38 | | /// |
| | 39 | | /// await messageSender.SendAsync(message1); |
| | 40 | | /// await messageSender.SendAsync(message2); |
| | 41 | | /// await messageSender.SendAsync(message3); |
| | 42 | | /// </code> |
| | 43 | | /// |
| | 44 | | /// Then retrieve the original objects using this client library as follows: |
| | 45 | | /// (By default <see cref="DataContractBinarySerializer"/> will be used to deserialize and retrieve the body. |
| | 46 | | /// If a serializer other than that was used, pass in the serializer explicitly.) |
| | 47 | | /// <code> |
| | 48 | | /// var message1 = await messageReceiver.ReceiveAsync(); |
| | 49 | | /// var returnedData1 = message1.GetBody<string>(); |
| | 50 | | /// |
| | 51 | | /// var message2 = await messageReceiver.ReceiveAsync(); |
| | 52 | | /// var returnedData2 = message1.GetBody<SampleObject>(); |
| | 53 | | /// |
| | 54 | | /// var message3 = await messageReceiver.ReceiveAsync(); |
| | 55 | | /// var returnedData3Bytes = message1.GetBody<byte[]>(); |
| | 56 | | /// Console.WriteLine($"Message3 String: {Encoding.UTF8.GetString(returnedData3Bytes)}"); |
| | 57 | | /// </code> |
| | 58 | | /// |
| | 59 | | /// ------------------------------------------------- |
| | 60 | | /// Scenarios to NOT use the GetBody Extension method: |
| | 61 | | /// ------------------------------------------------- |
| | 62 | | /// If message was sent using the WindowsAzure.Messaging client library as follows: |
| | 63 | | /// var message4 = new BrokeredMessage(new MemoryStream(Encoding.UTF8.GetBytes("contoso"))); |
| | 64 | | /// await messageSender.SendAsync(message4); |
| | 65 | | /// |
| | 66 | | /// Then retrieve the original objects using this client library as follows: |
| | 67 | | /// var message4 = await messageReceiver.ReceiveAsync(); |
| | 68 | | /// string returned = Encoding.UTF8.GetString(message4.Body); // Since message was sent as Stream, no deserializ |
| | 69 | | /// |
| | 70 | | /// </remarks> |
| | 71 | | public static class MessageInteropExtensions |
| | 72 | | { |
| | 73 | | /// <summary> |
| | 74 | | /// Deserializes the body of a message that was serialized using XmlObjectSerializer |
| | 75 | | /// </summary> |
| | 76 | | public static T GetBody<T>(this Message message, XmlObjectSerializer serializer = null) |
| | 77 | | { |
| 8 | 78 | | if(message == null) |
| | 79 | | { |
| 0 | 80 | | throw new ArgumentNullException(nameof(message)); |
| | 81 | | } |
| | 82 | |
|
| 8 | 83 | | if(message.SystemProperties.BodyObject != null) |
| | 84 | | { |
| 4 | 85 | | return (T)message.SystemProperties.BodyObject; |
| | 86 | | } |
| | 87 | |
|
| 4 | 88 | | if(message.Body == null || message.Body.Length == 0) |
| | 89 | | { |
| 0 | 90 | | return default; |
| | 91 | | } |
| | 92 | |
|
| 4 | 93 | | if(serializer == null) |
| | 94 | | { |
| 0 | 95 | | serializer = DataContractBinarySerializer<T>.Instance; |
| | 96 | | } |
| | 97 | |
|
| 4 | 98 | | using (var memoryStream = new MemoryStream(message.Body.Length)) |
| | 99 | | { |
| 4 | 100 | | memoryStream.Write(message.Body, 0, message.Body.Length); |
| 4 | 101 | | memoryStream.Flush(); |
| 4 | 102 | | memoryStream.Position = 0; |
| 4 | 103 | | return (T)serializer.ReadObject(memoryStream); |
| | 104 | | } |
| 4 | 105 | | } |
| | 106 | | } |
| | 107 | | } |