Esempio n. 1
0
 public Task SendReply(object replyContent, SendOptions options = default(SendOptions),
     CancellationToken cancellationToken = default(CancellationToken))
 {
     if (replyContent == null) throw new ArgumentNullException("replyContent");
     return _bus.SendReply(this, replyContent, options, cancellationToken);
 }
Esempio 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);
        }
Esempio 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);
        }
Esempio n. 4
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;
        }
Esempio n. 5
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;
        }
Esempio n. 6
0
        private Message BuildMessage(object content, IMessageHeaders suppliedHeaders, SendOptions options)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }
            var headers = new MessageHeaders(suppliedHeaders)
            {
                MessageId   = MessageId.Generate(),
                Origination = _baseUri,
                Synchronous = options != null && options.Synchronous
            };

            if (options != null && options.TTL > TimeSpan.Zero)
            {
                headers.Expires = DateTime.UtcNow.Add(options.TTL);
            }

            var contentType = options?.ContentType;

            if (string.IsNullOrWhiteSpace(contentType))
            {
                contentType = _defaultContentType;
            }
            headers.ContentType = contentType;

            return(_messageMarshaller.Marshal(content, headers));
        }