-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
MessageInterOpExtensions.cs
107 lines (101 loc) · 5.08 KB
/
MessageInterOpExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.Azure.ServiceBus.InteropExtensions
{
using System;
using System.IO;
using System.Runtime.Serialization;
/// <summary>
/// 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
/// <see cref="DataContractBinarySerializer"/> (default serializer) or <see cref="DataContractSerializer"/>
/// when sending message. This class provides extension methods to deserialize
/// and retrieve the body of such messages.
/// </summary>
/// <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 <see cref="Message.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:
/// <code>
/// 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);
/// </code>
///
/// Then retrieve the original objects using this client library as follows:
/// (By default <see cref="DataContractBinarySerializer"/> will be used to deserialize and retrieve the body.
/// If a serializer other than that was used, pass in the serializer explicitly.)
/// <code>
/// 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)}");
/// </code>
///
/// -------------------------------------------------
/// 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.
///
/// </remarks>
public static class MessageInteropExtensions
{
/// <summary>
/// Deserializes the body of a message that was serialized using XmlObjectSerializer
/// </summary>
public static T GetBody<T>(this Message message, XmlObjectSerializer serializer = null)
{
if(message == null)
{
throw new ArgumentNullException(nameof(message));
}
if(message.SystemProperties.BodyObject != null)
{
return (T)message.SystemProperties.BodyObject;
}
if(message.Body == null || message.Body.Length == 0)
{
return default;
}
if(serializer == null)
{
serializer = DataContractBinarySerializer<T>.Instance;
}
using (var memoryStream = new MemoryStream(message.Body.Length))
{
memoryStream.Write(message.Body, 0, message.Body.Length);
memoryStream.Flush();
memoryStream.Position = 0;
return (T)serializer.ReadObject(memoryStream);
}
}
}
}