/
MessageMetadataRegistry.cs
238 lines (199 loc) · 8.2 KB
/
MessageMetadataRegistry.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
namespace NServiceBus.Unicast.Messages;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Logging;
using NServiceBus;
/// <summary>
/// Cache of message metadata.
/// </summary>
public class MessageMetadataRegistry
{
/// <summary>
/// Create a new <see cref="MessageMetadataRegistry"/> instance.
/// </summary>
/// <param name="isMessageType">The function delegate indicating whether a specific type is a message type.</param>
/// <param name="allowDynamicTypeLoading">When set to <c>true</c> the metadata registry will attempt to dynamically
/// load types by using <see cref="Type.GetType(string)"/>; otherwise no attempts will be made to load types
/// at runtime and all types must be explicitly loaded beforehand.</param>
public MessageMetadataRegistry(Func<Type, bool> isMessageType, bool allowDynamicTypeLoading)
{
this.isMessageType = isMessageType;
this.allowDynamicTypeLoading = allowDynamicTypeLoading;
}
/// <summary>
/// Retrieves the <see cref="MessageMetadata" /> for the specified type.
/// </summary>
/// <param name="messageType">The message type to retrieve metadata for.</param>
/// <returns>The <see cref="MessageMetadata" /> for the specified type.</returns>
public MessageMetadata GetMessageMetadata(Type messageType)
{
ArgumentNullException.ThrowIfNull(messageType);
if (messages.TryGetValue(messageType.TypeHandle, out var metadata))
{
return metadata;
}
if (isMessageType(messageType))
{
return RegisterMessageType(messageType);
}
var message = $"Could not find metadata for '{messageType.FullName}'.{Environment.NewLine}Ensure the following:{Environment.NewLine}1. '{messageType.FullName}' is included in initial scanning. {Environment.NewLine}2. '{messageType.FullName}' implements either 'IMessage', 'IEvent' or 'ICommand' or alternatively, if you don't want to implement an interface, you can use 'Unobtrusive Mode'.";
throw new Exception(message);
}
/// <summary>
/// Retrieves the <see cref="MessageMetadata" /> for the message identifier.
/// </summary>
/// <param name="messageTypeIdentifier">The message identifier to retrieve metadata for.</param>
/// <returns>The <see cref="MessageMetadata" /> for the specified type.</returns>
public MessageMetadata GetMessageMetadata(string messageTypeIdentifier)
{
ArgumentException.ThrowIfNullOrWhiteSpace(messageTypeIdentifier);
var cacheHit = cachedTypes.TryGetValue(messageTypeIdentifier, out var messageType);
if (!cacheHit)
{
messageType = GetType(messageTypeIdentifier);
if (messageType == null)
{
foreach (var item in messages.Values)
{
var messageTypeFullName = GetMessageTypeNameWithoutAssembly(messageTypeIdentifier);
if (item.MessageType.FullName == messageTypeIdentifier ||
item.MessageType.FullName == messageTypeFullName)
{
Logger.DebugFormat("Message type: '{0}' was mapped to '{1}'", messageTypeIdentifier, item.MessageType.AssemblyQualifiedName);
cachedTypes[messageTypeIdentifier] = item.MessageType;
return item;
}
}
Logger.DebugFormat("Message type: '{0}' No match on known messages", messageTypeIdentifier);
}
cachedTypes[messageTypeIdentifier] = messageType;
}
if (messageType == null)
{
return null;
}
if (messages.TryGetValue(messageType.TypeHandle, out var metadata))
{
return metadata;
}
if (isMessageType(messageType))
{
return RegisterMessageType(messageType);
}
Logger.WarnFormat("Message header '{0}' was mapped to type '{1}' but that type was not found in the message registry, ensure the same message registration conventions are used in all endpoints, especially if using unobtrusive mode. ", messageType, messageType.FullName);
return null;
}
/// <summary>
/// Retrieves all known messages <see cref="MessageMetadata" />.
/// </summary>
/// <returns>An array of <see cref="MessageMetadata" /> for all known message.</returns>
public MessageMetadata[] GetAllMessages() => messages.Values.ToArray();
static string GetMessageTypeNameWithoutAssembly(string messageTypeIdentifier)
{
var typeParts = messageTypeIdentifier.Split(',');
if (typeParts.Length > 1)
{
return typeParts[0]; //Take the type part only
}
return messageTypeIdentifier;
}
Type GetType(string messageTypeIdentifier)
{
if (allowDynamicTypeLoading)
{
try
{
return Type.GetType(messageTypeIdentifier);
}
catch (Exception ex)
{
Logger.Warn($"Message type identifier '{messageTypeIdentifier}' could not be loaded", ex);
}
}
else
{
Logger.Warn($"Unknown message type identifier '{messageTypeIdentifier}'. Dynamic type loading is disabled. Make sure the type is loaded before starting the endpoint or enable dynamic type loading.");
}
return null;
}
internal void RegisterMessageTypesFoundIn(IList<Type> availableTypes)
{
foreach (var type in availableTypes)
{
if (isMessageType(type))
{
RegisterMessageType(type);
continue;
}
foreach (var messageType in GetHandledMessageTypes(type))
{
RegisterMessageType(messageType);
}
}
}
static IEnumerable<Type> GetHandledMessageTypes(Type messageHandlerType)
{
if (messageHandlerType.IsAbstract || messageHandlerType.IsGenericTypeDefinition)
{
yield break;
}
foreach (var handlerInterface in messageHandlerType.GetInterfaces())
{
if (handlerInterface.IsGenericType && handlerInterface.GetGenericTypeDefinition() == IHandleMessagesType)
{
yield return handlerInterface.GetGenericArguments()[0];
}
}
}
MessageMetadata RegisterMessageType(Type messageType)
{
//get the parent types
var parentMessages = GetParentTypes(messageType)
.Where(t => isMessageType(t))
.OrderByDescending(PlaceInMessageHierarchy);
var metadata = new MessageMetadata(messageType, new[]
{
messageType
}.Concat(parentMessages).ToArray());
messages[messageType.TypeHandle] = metadata;
cachedTypes.TryAdd(messageType.AssemblyQualifiedName, messageType);
return metadata;
}
static int PlaceInMessageHierarchy(Type type)
{
if (type.IsInterface)
{
return type.GetInterfaces().Length;
}
var result = 0;
while (type.BaseType != null)
{
result++;
type = type.BaseType;
}
return result;
}
static IEnumerable<Type> GetParentTypes(Type type)
{
foreach (var i in type.GetInterfaces())
{
yield return i;
}
// return all inherited types
var currentBaseType = type.BaseType;
var objectType = typeof(object);
while (currentBaseType != null && currentBaseType != objectType)
{
yield return currentBaseType;
currentBaseType = currentBaseType.BaseType;
}
}
readonly Func<Type, bool> isMessageType;
readonly bool allowDynamicTypeLoading;
readonly ConcurrentDictionary<RuntimeTypeHandle, MessageMetadata> messages = new ConcurrentDictionary<RuntimeTypeHandle, MessageMetadata>();
readonly ConcurrentDictionary<string, Type> cachedTypes = new ConcurrentDictionary<string, Type>();
static readonly Type IHandleMessagesType = typeof(IHandleMessages<>);
static readonly ILog Logger = LogManager.GetLogger<MessageMetadataRegistry>();
}