Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        private Message BuildMessage(object content, IMessageHeaders suppliedHeaders = null,
            SendOptions options = default(SendOptions))
        {
            if (content == null) throw new ArgumentNullException("content");
            var messageName = _messageNamingService.GetNameForType(content.GetType());
            var headers = new MessageHeaders(suppliedHeaders)
            {
                MessageId = MessageId.Generate(),
                MessageName = messageName,
                Origination = _baseUri,
                Importance = options.Importance
            };

            var contentType = options.ContentType;
            if (string.IsNullOrWhiteSpace(contentType))
            {
                contentType = "application/json";
            }
            headers.ContentType = contentType;

            var serializer = _serializationService.GetSerializer(headers.ContentType);
            var serializedContent = serializer.Serialize(content);
            return new Message(headers, serializedContent);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Publishes <paramref name="content" /> to the specified
        ///     <paramref name="topic" />.
        /// </summary>
        /// <param name="content">The message to publish.</param>
        /// <param name="topic">The topic to which the message should be published.</param>
        /// <param name="cancellationToken">An optional cancellation token</param>
        public async Task Publish(object content, TopicName topic,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            CheckDisposed();
            if (!_topics.Contains(topic)) throw new TopicNotFoundException(topic);

            var prototypicalHeaders = new MessageHeaders
            {
                Topic = topic,
                Published = DateTime.UtcNow
            };

            var message = BuildMessage(content, prototypicalHeaders);
            if (_messageJournalingService != null)
            {
                await _messageJournalingService.MessagePublished(message, cancellationToken);
            }

            await _transportService.PublishMessage(message, topic, cancellationToken);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Sends <paramref name="content" /> to a single caller-specified
        ///     endpoint <paramref name="endpointAddress" />.
        /// </summary>
        /// <param name="content">The message to send.</param>
        /// <param name="endpointAddress">The URI of the endpoint to which the message should be sent.</param>
        /// <param name="credentials">Optional credentials for authenticating with the endpoint
        /// at the specified URI.</param>
        /// <param name="options">Optional settings that influence how the message is sent.</param>
        /// <param name="cancellationToken">An optional cancellation token</param>
        public async Task<ISentMessage> Send(object content, Uri endpointAddress, IEndpointCredentials credentials = null,
            SendOptions options = default(SendOptions),
            CancellationToken cancellationToken = default(CancellationToken))
        {
            CheckDisposed();

            if (content == null) throw new ArgumentNullException("content");
            if (endpointAddress == null) throw new ArgumentNullException("endpointAddress");

            var headers = new MessageHeaders
            {
                Destination = endpointAddress
            };

            var message = BuildMessage(content, headers, options);

            Log.DebugFormat("Sending message ID {0} to \"{2}\"...",
                message.Headers.MessageId, endpointAddress);

            // 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);
            await _transportService.SendMessage(message, credentials, cancellationToken);
            return sentMessage;
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     Sends a <paramref name="content" /> to default configured endpoints.
        /// </summary>
        /// <param name="content">The content to send.</param>
        /// <param name="options">Optional settings that influence how the message is sent.</param>
        /// <param name="cancellationToken">An optional cancellation token</param>
        public async Task<ISentMessage> Send(object content, SendOptions options = default(SendOptions),
            CancellationToken cancellationToken = default(CancellationToken))
        {
            CheckDisposed();

            if (content == null) throw new ArgumentNullException("content");

            var prototypicalMessage = BuildMessage(content, options: options);
            var endpoints = GetEndpointsForMessage(prototypicalMessage);
            var transportTasks = new List<Task>();

            // 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(prototypicalMessage);

            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach (var kvp in endpoints)
            {
                var endpointName = kvp.Key;
                var endpoint = kvp.Value;
                var credentials = endpoint.Credentials;
                var perEndpointHeaders = new MessageHeaders(prototypicalMessage.Headers)
                {
                    Destination = endpoint.Address
                };

                Log.DebugFormat("Sending message ID {0} to endpoint \"{1}\" ({2})...",
                    prototypicalMessage.Headers.MessageId, endpointName, endpoint.Address);

                var addressedMessage = new Message(perEndpointHeaders, prototypicalMessage.Content);
                transportTasks.Add(_transportService.SendMessage(addressedMessage, credentials, cancellationToken));
            }
            await Task.WhenAll(transportTasks);
            return sentMessage;
        }