@@ -18,13 +18,15 @@ public abstract class MessageBusBase<TOptions> : IMessageBus, IDisposable where
1818 protected readonly TOptions _options ;
1919 protected readonly ILogger _logger ;
2020 protected readonly ISerializer _serializer ;
21+ protected readonly ITypeNameSerializer _typeNameSerializer ;
2122 private bool _isDisposed ;
2223
2324 public MessageBusBase ( TOptions options ) {
2425 _options = options ?? throw new ArgumentNullException ( nameof ( options ) ) ;
2526 var loggerFactory = options ? . LoggerFactory ?? NullLoggerFactory . Instance ;
2627 _logger = loggerFactory . CreateLogger ( GetType ( ) ) ;
2728 _serializer = options . Serializer ?? DefaultSerializer . Instance ;
29+ _typeNameSerializer = options . TypeNameSerializer ?? new DefaultTypeNameSerializer ( _logger ) ;
2830 MessageBusId = _options . Topic + Guid . NewGuid ( ) . ToString ( "N" ) . Substring ( 10 ) ;
2931 _messageBusDisposedCancellationTokenSource = new CancellationTokenSource ( ) ;
3032 }
@@ -39,32 +41,12 @@ public async Task PublishAsync(Type messageType, object message, TimeSpan? delay
3941 await PublishImplAsync ( messageType , message , delay , cancellationToken ) . AnyContext ( ) ;
4042 }
4143
42- private readonly ConcurrentDictionary < Type , string > _mappedMessageTypesCache = new ConcurrentDictionary < Type , string > ( ) ;
4344 protected string GetMappedMessageType ( Type messageType ) {
44- return _mappedMessageTypesCache . GetOrAdd ( messageType , type => {
45- var reversedMap = _options . MessageTypeMappings . ToDictionary ( kvp => kvp . Value , kvp => kvp . Key ) ;
46- if ( reversedMap . ContainsKey ( type ) )
47- return reversedMap [ type ] ;
48-
49- return String . Concat ( messageType . FullName , ", " , messageType . Assembly . GetName ( ) . Name ) ;
50- } ) ;
45+ return _typeNameSerializer . Serialize ( messageType ) ;
5146 }
5247
53- private readonly ConcurrentDictionary < string , Type > _knownMessageTypesCache = new ConcurrentDictionary < string , Type > ( ) ;
5448 protected Type GetMappedMessageType ( string messageType ) {
55- return _knownMessageTypesCache . GetOrAdd ( messageType , type => {
56- if ( _options . MessageTypeMappings != null && _options . MessageTypeMappings . ContainsKey ( type ) )
57- return _options . MessageTypeMappings [ type ] ;
58-
59- try {
60- return Type . GetType ( type ) ;
61- } catch ( Exception ex ) {
62- if ( _logger . IsEnabled ( LogLevel . Warning ) )
63- _logger . LogWarning ( ex , "Error getting message body type: {MessageType}" , type ) ;
64-
65- return null ;
66- }
67- } ) ;
49+ return _typeNameSerializer . Deserialize ( messageType ) ;
6850 }
6951
7052 protected virtual Task EnsureTopicSubscriptionAsync < T > ( CancellationToken cancellationToken ) where T : class => Task . CompletedTask ;
0 commit comments