-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
NServiceBusTypeCache.cs
53 lines (45 loc) · 1.71 KB
/
NServiceBusTypeCache.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
namespace MassTransit.Interop.NServiceBus.Metadata
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using MassTransit.Metadata;
public class NServiceBusTypeCache<T> :
INServiceBusTypeMetadataCache<T>
{
readonly Lazy<string[]> _messageTypeNames;
NServiceBusTypeCache()
{
_messageTypeNames = new Lazy<string[]>(() => GetMessageTypeNames().ToArray());
}
/// <summary>
/// The names of all the message types supported by the message type
/// </summary>
string[] INServiceBusTypeMetadataCache<T>.MessageTypeNames => _messageTypeNames.Value;
public static string[] MessageTypeNames => Cached.Metadata.Value.MessageTypeNames;
static IEnumerable<string> GetMessageTypeNames()
{
foreach (var messageType in TypeMetadataCache<T>.MessageTypes)
{
switch (messageType.Name)
{
case "IEvent":
case "IMessage":
case "ICommand":
if (!"NServiceBus".Equals(messageType.Namespace))
yield return messageType.AssemblyQualifiedName;
break;
default:
yield return messageType.AssemblyQualifiedName;
break;
}
}
}
static class Cached
{
internal static readonly Lazy<INServiceBusTypeMetadataCache<T>> Metadata = new Lazy<INServiceBusTypeMetadataCache<T>>(
() => new NServiceBusTypeCache<T>(), LazyThreadSafetyMode.PublicationOnly);
}
}
}