Exemplo n.º 1
0
        /// <inheritdoc />
        /// <summary>
        /// Handles a message that is received off of a queue
        /// </summary>
        /// <param name="message">The message that was received</param>
        /// <param name="context">The context in which the message was dequeued</param>
        /// <param name="cancellationToken">A cancellation token provided by the
        /// queue that can be used to cancel message processing</param>
        /// <returns>Returns a task that completes when the message is processed</returns>
        async Task IQueueListener.MessageReceived(Message message, IQueuedMessageContext context,
                                                  CancellationToken cancellationToken)
        {
            IEndpointCredentials credentials = null;

            if (_endpoints.TryGetEndpointByAddress(message.Headers.Destination, out IEndpoint endpoint))
            {
                credentials = endpoint.Credentials;
            }
            cancellationToken.ThrowIfCancellationRequested();
            Thread.CurrentPrincipal = context.Principal;
            await TransportMessage(message, credentials, cancellationToken);

            await context.Acknowledge();
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public Task <ISentMessage> Send(object content, Uri endpointAddress,
                                        SendOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            CheckDisposed();

            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }
            if (endpointAddress == null)
            {
                throw new ArgumentNullException(nameof(endpointAddress));
            }

            var myOptions = options ?? _defaultSendOptions;
            var headers   = new MessageHeaders
            {
                Destination = endpointAddress,
                Sent        = DateTime.UtcNow
            };

            var message     = BuildMessage(content, headers, myOptions);
            var credentials = myOptions.Credentials;

            if (credentials == null && _endpoints.TryGetEndpointByAddress(endpointAddress, out IEndpoint knownEndpoint))
            {
                credentials = knownEndpoint.Credentials;
            }

            // Create the sent message before transporting it in order to ensure that the
            // reply stream is cached before any replies arrive.
            var sentMessage       = _replyHub.CreateSentMessage(message);
            var sentMessageSource = SendMessage(message, credentials, cancellationToken)
                                    .GetCompletionSource(sentMessage, cancellationToken);

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

            return(sentMessageSource.Task);
        }