Esempio n. 1
0
        /// <inheritdoc />
        public async Task MessageReceived(Message message, IQueuedMessageContext context,
                                          CancellationToken cancellationToken = default(CancellationToken))
        {
            var messageContext = new BusMessageContext(_bus, context.Headers, context.Principal);
            await _messageHandler.HandleMessage(_messageHandlers, message, messageContext, cancellationToken);

            if (messageContext.MessageAcknowledged)
            {
                await context.Acknowledge();
            }
        }
        public async Task MessageReceived(Message message, IQueuedMessageContext context,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var messageContext = new BusMessageContext(_bus, context.Headers, context.SenderPrincipal);
            await MessageHandler.HandleMessage(_messageNamingService, _serializationService, _messageHandlers,
                message, messageContext, cancellationToken);

            if (messageContext.MessageAcknowledged)
            {
                await context.Acknowledge();
            }
        }
Esempio n. 3
0
        private async Task HandleMessageImmediately(Message message, IPrincipal senderPrincipal, CancellationToken cancellationToken)
        {
            var matchingRules = _handlingRules
                                .Where(r => r.Specification.IsSatisfiedBy(message))
                                .ToList();

            if (!matchingRules.Any())
            {
                await _diagnosticService.EmitAsync(
                    new DiagnosticEventBuilder(this, DiagnosticEventType.NoMatchingHandlingRules)
                {
                    Message = message
                }.Build(), cancellationToken);

                throw new MessageNotAcknowledgedException("Message does not match any configured handling rules");
            }

            var messageContext  = new BusMessageContext(this, message.Headers, senderPrincipal);
            var handlers        = matchingRules.Select(rule => rule.MessageHandler).ToList();
            var autoAcknowledge = matchingRules.Any(rule => rule.QueueOptions?.AutoAcknowledge ?? false);

            await _messageHandler.HandleMessage(handlers, message, messageContext, _cancellationTokenSource.Token);

            var acknowledged = messageContext.MessageAcknowledged || autoAcknowledge;

            if (!acknowledged)
            {
                await _diagnosticService.EmitAsync(
                    new DiagnosticEventBuilder(this, DiagnosticEventType.MessageNotAcknowledged)
                {
                    Message = message
                }.Build(), cancellationToken);

                throw new MessageNotAcknowledgedException("Message not acknowledged by any message handlers");
            }

            await _diagnosticService.EmitAsync(
                new DiagnosticEventBuilder(this, DiagnosticEventType.MessageAcknowledged)
            {
                Message = message
            }.Build(), cancellationToken);
        }
Esempio n. 4
0
        internal Task SendReply(BusMessageContext messageContext, object replyContent,
                                SendOptions options = default(SendOptions), CancellationToken cancellationToken = default(CancellationToken))
        {
            if (messageContext == null)
            {
                throw new ArgumentNullException(nameof(messageContext));
            }
            if (replyContent == null)
            {
                throw new ArgumentNullException(nameof(replyContent));
            }

            IEndpointCredentials credentials = null;
            var replyTo = messageContext.Headers.ReplyTo ?? messageContext.Headers.Origination;

            if (_endpoints.TryGetEndpointByAddress(replyTo, out IEndpoint replyToEndpoint))
            {
                credentials = replyToEndpoint.Credentials;
            }

            var headers = new MessageHeaders
            {
                Destination = messageContext.Headers.Origination,
                RelatedTo   = messageContext.Headers.MessageId
            };

            var replyMessage      = BuildMessage(replyContent, headers, options);
            var sentMessageSource = SendMessage(replyMessage, credentials, cancellationToken)
                                    .GetCompletionSource(cancellationToken);

            _diagnosticService.Emit(
                new DiagnosticEventBuilder(this, DiagnosticEventType.MessageSent)
            {
                Message = replyMessage
            }.Build());

            return(sentMessageSource.Task);
        }
Esempio n. 5
0
        private async Task HandleMessageImmediately(Message message, SenderPrincipal senderPrincipal, bool isReply)
        {
            var messageContext = new BusMessageContext(this, message.Headers, senderPrincipal);
            var handlers = _handlingRules
                .Where(r => r.Specification.IsSatisfiedBy(message))
                .Select(rule => rule.MessageHandler)
                .ToList();

            if (!handlers.Any() && isReply)
            {
                // TODO: Figure out what to do here.
                return;
            }

            await MessageHandler.HandleMessage(_messageNamingService, _serializationService,
                handlers, message, messageContext, _cancellationTokenSource.Token);

            if (!messageContext.MessageAcknowledged)
            {
                throw new MessageNotAcknowledgedException();
            }
        }
Esempio n. 6
0
        internal async Task SendReply(BusMessageContext messageContext, object replyContent,
            SendOptions options = default(SendOptions), CancellationToken cancellationToken = default(CancellationToken))
        {
            if (messageContext == null) throw new ArgumentNullException("messageContext");
            if (replyContent == null) throw new ArgumentNullException("replyContent");

            IEndpoint replyToEndpoint;
            IEndpointCredentials credentials = null;
            var replyTo = messageContext.Headers.ReplyTo ?? messageContext.Headers.Origination;
            if (_endpoints.TryGetEndpointByAddress(replyTo, out replyToEndpoint))
            {
                credentials = replyToEndpoint.Credentials;
            }

            var headers = new MessageHeaders
            {
                Destination = messageContext.Headers.Origination,
                RelatedTo = messageContext.Headers.MessageId
            };
            var replyMessage = BuildMessage(replyContent, headers, options);
            await _transportService.SendMessage(replyMessage, credentials, cancellationToken);
        }