internal async Task <AmqpIotOutcome> SendMessageAsync(Message message, CancellationToken cancellationToken)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, message, nameof(SendMessageAsync));
            }

            // After this message is sent, we will return the outcome that has no references to the message
            // So it can safely be disposed.
            using AmqpMessage amqpMessage = AmqpIotMessageConverter.MessageToAmqpMessage(message);
            Outcome outcome = await SendAmqpMessageAsync(amqpMessage, cancellationToken).ConfigureAwait(false);

            if (Logging.IsEnabled)
            {
                Logging.Exit(this, message, nameof(SendMessageAsync));
            }

            return(new AmqpIotOutcome(outcome));
        }
예제 #2
0
        internal async Task <AmqpIotOutcome> SendMessagesAsync(IEnumerable <Message> messages, TimeSpan timeout)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, nameof(SendMessagesAsync));
            }

            // List to hold messages in AMQP friendly format
            var messageList = new List <Data>();

            foreach (Message message in messages)
            {
                using AmqpMessage amqpMessage = AmqpIotMessageConverter.MessageToAmqpMessage(message);
                var data = new Data
                {
                    Value = AmqpIotMessageConverter.ReadStream(amqpMessage.ToStream()),
                };
                messageList.Add(data);
            }

            Outcome outcome;

            using (AmqpMessage amqpMessage = AmqpMessage.Create(messageList))
            {
                amqpMessage.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat;
                outcome = await SendAmqpMessageAsync(amqpMessage, timeout).ConfigureAwait(false);
            }

            var amqpIotOutcome = new AmqpIotOutcome(outcome);

            amqpIotOutcome.ThrowIfNotAccepted();

            if (Logging.IsEnabled)
            {
                Logging.Exit(this, nameof(SendMessagesAsync));
            }

            return(amqpIotOutcome);
        }
        internal async Task <AmqpIotOutcome> SendMessagesAsync(IEnumerable <Message> messages, CancellationToken cancellationToken)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, nameof(SendMessagesAsync));
            }

            cancellationToken.ThrowIfCancellationRequested();

            // List to hold messages in AMQP friendly format
            var messageList = new List <Data>(messages.Count());

            foreach (Message message in messages)
            {
                using AmqpMessage amqpMessage = AmqpIotMessageConverter.MessageToAmqpMessage(message);
                var data = new Data
                {
                    Value = AmqpIotMessageConverter.ReadStream(amqpMessage.ToStream()),
                };
                messageList.Add(data);
            }

            using var batchMessage     = AmqpMessage.Create(messageList);
            batchMessage.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat;
            Outcome outcome = await SendAmqpMessageAsync(batchMessage, cancellationToken).ConfigureAwait(false);

            var amqpIotOutcome = new AmqpIotOutcome(outcome);

            amqpIotOutcome.ThrowIfNotAccepted();

            if (Logging.IsEnabled)
            {
                Logging.Exit(this, nameof(SendMessagesAsync));
            }

            return(amqpIotOutcome);
        }