protected async Task SendMessageToSubscribersAsync(MessageBusData message, ISerializer serializer) { Type messageType = GetMessageBodyType(message); if (messageType == null) { return; } var subscribers = _subscribers.Values.Where(s => s.IsAssignableFrom(messageType)).ToList(); if (subscribers.Count == 0) { _logger.Trace(() => $"Done sending message to 0 subscribers for message type {messageType.Name}."); return; } object body; try { body = await serializer.DeserializeAsync(message.Data, messageType).AnyContext(); } catch (Exception ex) { _logger.Error(ex, "Error while deserializing messsage body: {0}", ex.Message); return; } if (body == null) { _logger.Warn("Unable to send null message for type {0}", messageType.Name); return; } await SendMessageToSubscribersAsync(subscribers, messageType, body).AnyContext(); }
protected Type GetMessageBodyType(MessageBusData message) { if (message == null || message.Type == null) { return(null); } return(GetMappedMessageType(message.Type)); }
protected void SendMessageToSubscribers(MessageBusData message, ISerializer serializer) { var messageType = GetMessageBodyType(message); if (messageType == null) { return; } SendMessageToSubscribers(messageType, message.Data, serializer); }
protected void SendMessageToSubscribers(MessageBusData message, ISerializer serializer) { Type messageType = GetMessageBodyType(message); if (!(messageType == (Type)null)) { List <Subscriber> list = Enumerable.ToList <Subscriber>(Enumerable.Where <Subscriber>((IEnumerable <Subscriber>)_subscribers.Values, (Func <Subscriber, bool>)((Subscriber s) => s.IsAssignableFrom(messageType)))); if (list.Count == 0) { if (_logger.IsEnabled(0)) { LoggerExtensions.LogTrace(_logger, "Done sending message to 0 subscribers for message type {MessageType}.", new object[1] { messageType.Name }); } } else { object obj; try { obj = serializer.Deserialize(message.Data, messageType); } catch (Exception ex) { if (_logger.IsEnabled(LogLevel.Warning)) { LoggerExtensions.LogWarning(_logger, ex, "Error deserializing messsage body: {Message}", new object[1] { ex.Message }); } return; } if (obj == null) { if (_logger.IsEnabled(LogLevel.Warning)) { LoggerExtensions.LogWarning(_logger, "Unable to send null message for type {MessageType}", new object[1] { messageType.Name }); } } else { SendMessageToSubscribers(list, messageType, obj); } } } }
protected Type GetMessageBodyType(MessageBusData message) { if (message?.Type == null) { return(null); } return(_knownMessageTypesCache.GetOrAdd(message.Type, type => { try { return Type.GetType(type); } catch (Exception ex) { _logger.Error(ex, "Error getting message body type: {0}", type); return null; } })); }
protected void SendMessageToSubscribers(MessageBusData message, ISerializer serializer) { var messageType = GetMessageBodyType(message); if (messageType == null) { return; } var subscribers = _subscribers.Values.Where(s => s.IsAssignableFrom(messageType)).ToList(); if (subscribers.Count == 0) { if (_logger.IsEnabled(LogLevel.Trace)) { _logger.LogTrace("Done sending message to 0 subscribers for message type {MessageType}.", messageType.Name); } return; } object body; try { body = serializer.Deserialize(message.Data, messageType); } catch (Exception ex) { if (_logger.IsEnabled(LogLevel.Warning)) { _logger.LogWarning(ex, "Error deserializing messsage body: {Message}", ex.Message); } return; } if (body == null) { if (_logger.IsEnabled(LogLevel.Warning)) { _logger.LogWarning("Unable to send null message for type {MessageType}", messageType.Name); } return; } SendMessageToSubscribers(subscribers, messageType, body); }
protected Type GetMessageBodyType(MessageBusData message) { if (message?.Type == null) { return(null); } return(_knownMessageTypesCache.GetOrAdd(message.Type, type => { try { return Type.GetType(type); } catch (Exception ex) { if (_logger.IsEnabled(LogLevel.Warning)) { _logger.LogWarning(ex, "Error getting message body type: {MessageType}", type); } return null; } })); }