/
MsmqUtilities.cs
303 lines (253 loc) · 10.4 KB
/
MsmqUtilities.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
namespace NServiceBus.Transports.Msmq
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Messaging;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Logging;
/// <summary>
/// MSMQ-related utility functions
/// </summary>
public class MsmqUtilities
{
/// <summary>
/// Turns a '@' separated value into a full path.
/// Format is 'queue@machine', or 'queue@ipaddress'
/// </summary>
public static string GetFullPath(Address value)
{
IPAddress ipAddress;
if (IPAddress.TryParse(value.Machine, out ipAddress))
{
return PREFIX_TCP + MsmqQueueCreator.GetFullPathWithoutPrefix(value);
}
return PREFIX + MsmqQueueCreator.GetFullPathWithoutPrefix(value);
}
/// <summary>
/// Gets the name of the return address from the provided value.
/// If the target includes a machine name, uses the local machine name in the returned value
/// otherwise uses the local IP address in the returned value.
/// </summary>
public static string GetReturnAddress(string value, string target)
{
return GetReturnAddress(Address.Parse(value), Address.Parse(target));
}
/// <summary>
/// Gets the name of the return address from the provided value.
/// If the target includes a machine name, uses the local machine name in the returned value
/// otherwise uses the local IP address in the returned value.
/// </summary>
public static string GetReturnAddress(Address value, Address target)
{
var machine = target.Machine;
IPAddress targetIpAddress;
//see if the target is an IP address, if so, get our own local ip address
if (IPAddress.TryParse(machine, out targetIpAddress))
{
if (string.IsNullOrEmpty(localIp))
{
localIp = LocalIpAddress(targetIpAddress);
}
return PREFIX_TCP + localIp + PRIVATE + value.Queue;
}
return PREFIX + MsmqQueueCreator.GetFullPathWithoutPrefix(value);
}
static string LocalIpAddress(IPAddress targetIpAddress)
{
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
var availableAddresses =
networkInterfaces.Where(
ni =>
ni.OperationalStatus == OperationalStatus.Up &&
ni.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.SelectMany(ni => ni.GetIPProperties().UnicastAddresses).ToList();
var firstWithMatchingFamily =
availableAddresses.FirstOrDefault(a => a.Address.AddressFamily == targetIpAddress.AddressFamily);
if (firstWithMatchingFamily != null)
{
return firstWithMatchingFamily.Address.ToString();
}
var fallbackToDifferentFamily = availableAddresses.FirstOrDefault();
if (fallbackToDifferentFamily != null)
{
return fallbackToDifferentFamily.Address.ToString();
}
return "127.0.0.1";
}
/// <summary>
/// Gets an independent address for the queue in the form:
/// queue@machine.
/// </summary>
public static Address GetIndependentAddressForQueue(MessageQueue q)
{
if (q == null)
{
return null;
}
var arr = q.FormatName.Split('\\');
var queueName = arr[arr.Length - 1];
var directPrefixIndex = arr[0].IndexOf(DIRECTPREFIX);
if (directPrefixIndex >= 0)
{
return new Address(queueName, arr[0].Substring(directPrefixIndex + DIRECTPREFIX.Length));
}
var tcpPrefixIndex = arr[0].IndexOf(DIRECTPREFIX_TCP);
if (tcpPrefixIndex >= 0)
{
return new Address(queueName, arr[0].Substring(tcpPrefixIndex + DIRECTPREFIX_TCP.Length));
}
try
{
// the pessimistic approach failed, try the optimistic approach
arr = q.QueueName.Split('\\');
queueName = arr[arr.Length - 1];
return new Address(queueName, q.MachineName);
}
catch
{
throw new Exception("Could not translate format name to independent name: " + q.FormatName);
}
}
/// <summary>
/// Converts an MSMQ message to a TransportMessage.
/// </summary>
public static TransportMessage Convert(Message m)
{
var headers = DeserializeMessageHeaders(m);
var result = new TransportMessage(m.Id, headers)
{
Recoverable = m.Recoverable,
TimeToBeReceived = m.TimeToBeReceived,
ReplyToAddress = GetIndependentAddressForQueue(m.ResponseQueue),
CorrelationId = GetCorrelationId(m, headers)
};
if (Enum.IsDefined(typeof(MessageIntentEnum), m.AppSpecific))
{
result.MessageIntent = (MessageIntentEnum) m.AppSpecific;
}
m.BodyStream.Position = 0;
result.Body = new byte[m.BodyStream.Length];
m.BodyStream.Read(result.Body, 0, result.Body.Length);
return result;
}
static string GetCorrelationId(Message message, Dictionary<string, string> headers)
{
string correlationId;
if (headers.TryGetValue(Headers.CorrelationId, out correlationId))
{
return correlationId;
}
if (message.CorrelationId == "00000000-0000-0000-0000-000000000000\\0")
{
return null;
}
//msmq required the id's to be in the {guid}\{incrementing number} format so we need to fake a \0 at the end that the sender added to make it compatible
//The replace can be removed in v5 since only v3 messages will need this
return message.CorrelationId.Replace("\\0", "");
}
static Dictionary<string, string> DeserializeMessageHeaders(Message m)
{
var result = new Dictionary<string, string>();
if (m.Extension.Length == 0)
{
return result;
}
//This is to make us compatible with v3 messages that are affected by this bug:
//http://stackoverflow.com/questions/3779690/xml-serialization-appending-the-0-backslash-0-or-null-character
var extension = Encoding.UTF8.GetString(m.Extension).TrimEnd('\0');
object o;
using (var stream = new StringReader(extension))
{
using (var reader = XmlReader.Create(stream, new XmlReaderSettings
{
CheckCharacters = false
}))
{
o = headerSerializer.Deserialize(reader);
}
}
foreach (var pair in (List<HeaderInfo>)o)
{
if (pair.Key != null)
{
result.Add(pair.Key, pair.Value);
}
}
return result;
}
/// <summary>
/// Converts a TransportMessage to an Msmq message.
/// Doesn't set the ResponseQueue of the result.
/// </summary>
public static Message Convert(TransportMessage message)
{
var result = new Message();
if (message.Body != null)
{
result.BodyStream = new MemoryStream(message.Body);
}
AssignMsmqNativeCorrelationId(message, result);
result.Recoverable = message.Recoverable;
if (message.TimeToBeReceived < MessageQueue.InfiniteTimeout)
{
result.TimeToBeReceived = message.TimeToBeReceived;
}
using (var stream = new MemoryStream())
{
headerSerializer.Serialize(stream, message.Headers.Select(pair => new HeaderInfo
{
Key = pair.Key,
Value = pair.Value
}).ToList());
result.Extension = stream.ToArray();
}
result.AppSpecific = (int) message.MessageIntent;
return result;
}
static void AssignMsmqNativeCorrelationId(TransportMessage message, Message result)
{
if (string.IsNullOrEmpty(message.CorrelationId))
{
return;
}
Guid correlationId;
if (Guid.TryParse(message.CorrelationId, out correlationId))
{
//msmq required the id's to be in the {guid}\{incrementing number} format so we need to fake a \0 at the end to make it compatible
result.CorrelationId = message.CorrelationId + "\\0";
return;
}
try
{
if (message.CorrelationId.Contains("\\"))
{
var parts = message.CorrelationId.Split('\\');
int number;
if (parts.Count() == 2 && Guid.TryParse(parts.First(), out correlationId) &&
int.TryParse(parts[1], out number))
{
result.CorrelationId = message.CorrelationId;
}
}
}
catch (Exception ex)
{
Logger.Warn("Failed to assign a native correlation id for message: " + message.Id, ex);
}
}
const string DIRECTPREFIX = "DIRECT=OS:";
const string DIRECTPREFIX_TCP = "DIRECT=TCP:";
const string PREFIX_TCP = "FormatName:" + DIRECTPREFIX_TCP;
const string PREFIX = "FormatName:" + DIRECTPREFIX;
internal const string PRIVATE = "\\private$\\";
static string localIp;
static readonly XmlSerializer headerSerializer = new XmlSerializer(typeof(List<HeaderInfo>));
static ILog Logger = LogManager.GetLogger(typeof(MsmqUtilities));
}
}