async Task HandleMessage(string msg) { MessagingEnvelope <TMessage> deserializedMessage = null; try { deserializedMessage = _messageSerDes.DeserializeMessageEnvelope <TMessage>(msg, _subscriberOptions?.SerDes); } catch (Exception ex) { _logger.LogError( "MessageBusSubscriber encountered an error when deserializing a message from topic {TopicName}.\n {Error}", _topicName, ex); //TODO: push to DLQ } if (deserializedMessage != null) { foreach (var handler in _handlers.ToList()) { await handler(deserializedMessage); } } }
public async Task <IDisposable> SubscribeAsync <TMessage>(Func <MessagingEnvelope <TMessage>, Task> handler, MessagingSubscriberOptions options = null, CancellationToken cancellationToken = default) { var topicName = _topicRegistry.GetTopicForName(options?.TopicName) ?? _topicRegistry.GetTopicForMessageType(typeof(TMessage)); async Task MsgHandler(byte[] messageData) { _logger.LogDebug("Messaging subscriber received message from subject {Subject}", topicName); try { var messageEnvelope = _messageSerDes.DeserializeMessageEnvelope <TMessage>(messageData); await handler(messageEnvelope); } catch (Exception ex) { _logger.LogError( "Messaging consumer encountered an error when handling a message from subject {Subject}.\n {Error}", topicName, ex); //TODO: push to DLQ } } return(await _messagingTransport.SubscribeAsync(topicName, MsgHandler, options?.Transport, cancellationToken)); }
private async Task Handle(string message, CancellationToken cancellationToken = default) { MessagingEnvelope messageEnvelope = null; try { messageEnvelope = _messageSerDes.DeserializeMessageEnvelope(message, _subscriberOptions?.SerDes); } catch (Exception ex) { _logger.LogError( "MessagingTopicSubscriberService encountered an error when deserializing a message from topic {TopicName}.\n {Error}", _topic, ex); } using (var scope = _serviceProvider.CreateScope()) { var pipeline = scope.ServiceProvider.GetService <PipelineDelegate <MessagingEnvelope> >(); _messagingContextAccessor.MessagingContext = new MessagingContext(messageEnvelope); await pipeline(messageEnvelope, cancellationToken); } }