private async Task <DispatchResult> DispatchToListener(BasicDeliverEventArgs delivery, CancellationToken cancellationToken) { Message message = null; var acknowledged = false; try { var messageBody = _encoding.GetString(delivery.Body); using (var reader = new StringReader(messageBody)) using (var messageReader = new MessageReader(reader)) { message = await messageReader.ReadMessage(); if (_messageEncryptionService != null && message.IsEncrypted()) { message = await _messageEncryptionService.Decrypt(message); } var securityToken = message.Headers.SecurityToken; var principal = await _securityTokenService.NullSafeValidate(securityToken); // Remove sensitive information from the header, including security token var headers = new MessageHeaders(message.Headers.Sanitize()); if (headers.Received == default(DateTime)) { headers.Received = DateTime.UtcNow; } var context = new RabbitMQQueuedMessageContext(headers, principal); Thread.CurrentPrincipal = context.Principal; await _listener.MessageReceived(message, context, cancellationToken); acknowledged = context.Acknowledged || _autoAcknowledge; } } catch (OperationCanceledException) { } catch (Exception ex) { _diagnosticService.Emit( new RabbitMQEventBuilder(this, RabbitMQEventType.RabbitMQDeliveryError) { Message = message, Exception = ex, Queue = _queueName, ConsumerTag = delivery.ConsumerTag, DeliveryTag = delivery.DeliveryTag }.Build()); } return(new DispatchResult(message, acknowledged)); }
private async Task<bool> DispatchToListener(BasicDeliverEventArgs delivery, CancellationToken cancellationToken) { var messageBody = _encoding.GetString(delivery.Body); using (var reader = new StringReader(messageBody)) using (var messageReader = new MessageReader(reader)) { var principal = await messageReader.ReadPrincipal(); var message = await messageReader.ReadMessage(); var context = new RabbitMQQueuedMessageContext(message.Headers, principal); await _listener.MessageReceived(message, context, cancellationToken); return context.Acknowledged; } }