public override async Task SendMethodResponseAsync(MethodResponseInternal method, CancellationToken cancellationToken)
        {
            try
            {
                if (Logging.IsEnabled)
                {
                    Logging.Enter(this, method, cancellationToken, nameof(SendMethodResponseAsync));
                }

                await _internalRetryPolicy
                .ExecuteAsync(
                    async() =>
                {
                    await EnsureOpenedAsync(cancellationToken).ConfigureAwait(false);
                    await base.SendMethodResponseAsync(method, cancellationToken).ConfigureAwait(false);
                },
                    cancellationToken)
                .ConfigureAwait(false);
            }
            finally
            {
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, method, cancellationToken, nameof(SendMethodResponseAsync));
                }
            }
        }
Ejemplo n.º 2
0
        public override async Task SendMethodResponseAsync(MethodResponseInternal methodResponse, CancellationToken cancellationToken)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, methodResponse, cancellationToken, $"{nameof(SendMethodResponseAsync)}");
            }

            try
            {
                cancellationToken.ThrowIfCancellationRequested();

                Outcome outcome;
                using (AmqpMessage amqpMessage = methodResponse.ToAmqpMessage())
                {
                    outcome = await _amqpUnit.SendMethodResponseAsync(amqpMessage, _operationTimeout).ConfigureAwait(false);
                }
                if (outcome.DescriptorCode != Accepted.Code)
                {
                    throw AmqpErrorMapper.GetExceptionFromOutcome(outcome);
                }
            }
            finally
            {
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, methodResponse, cancellationToken, $"{nameof(SendMethodResponseAsync)}");
                }
            }
        }
        public override async Task SendMethodResponseAsync(MethodResponseInternal method, CancellationToken cancellationToken)
        {
            try
            {
                if (Logging.IsEnabled)
                {
                    Logging.Enter(this, method, cancellationToken, $"{nameof(RetryDelegatingHandler)}.{nameof(SendMethodResponseAsync)}");
                }

                var sendState = new SendMessageState();
                await this.internalRetryPolicy.ExecuteAsync(() =>
                {
                    return(base.SendMethodResponseAsync(method, cancellationToken));
                }, cancellationToken).ConfigureAwait(false);
            }
            catch (IotHubClientTransientException ex)
            {
                GetNormalizedIotHubException(ex).Throw();
            }
            finally
            {
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, method, cancellationToken, $"{nameof(RetryDelegatingHandler)}.{nameof(SendMethodResponseAsync)}");
                }
            }
        }
        public override async Task SendMethodResponseAsync(MethodResponseInternal methodResponse, CancellationToken cancellationToken)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, methodResponse, cancellationToken, nameof(SendMethodResponseAsync));
            }

            try
            {
                cancellationToken.ThrowIfCancellationRequested();

                using var ctb = new CancellationTokenBundle(_operationTimeout, cancellationToken);
                AmqpIotOutcome amqpIotOutcome = await _amqpUnit
                                                .SendMethodResponseAsync(methodResponse, ctb.Token)
                                                .ConfigureAwait(false);

                if (amqpIotOutcome != null)
                {
                    amqpIotOutcome.ThrowIfNotAccepted();
                }
            }
            finally
            {
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, methodResponse, cancellationToken, nameof(SendMethodResponseAsync));
                }
            }
        }
        public async Task MqttTransportHandlerSendMethodResponseAsyncSendsMessage()
        {
            // arrange
            IChannel channel;
            var      responseBytes = System.Text.Encoding.UTF8.GetBytes(fakeMethodResponseBody);
            var      transport     = CreateTransportHandlerWithMockChannel(out channel);
            var      response      = new MethodResponseInternal(responseBytes, fakeResponseId, statusSuccess);

            // act
            transport.OnConnected();
            await transport.SendMethodResponseAsync(response, CancellationToken.None).ConfigureAwait(false);

            // assert
            MessageMatcher matches = (msg) =>
            {
                using (StreamReader reader = new StreamReader(msg.GetBodyStream(), System.Text.Encoding.UTF8))
                {
                    string body = reader.ReadToEnd();

                    return(fakeMethodResponseBody.Equals(body) &&
                           msg.MqttTopicName.Equals("$iothub/methods/res/" + statusSuccess + "/?$rid=" + fakeResponseId));
                }
            };
            await channel
            .Received()
            .WriteAndFlushAsync(Arg.Is <Message>(msg => matches(msg))).ConfigureAwait(false);
        }
Ejemplo n.º 6
0
        public override async Task SendMethodResponseAsync(MethodResponseInternal methodResponse, CancellationToken cancellationToken)
        {
            // Codes_SRS_CSHARP_MQTT_TRANSPORT_18_005:  `SendMethodResponseAsync` shall allocate a `Message` object containing the method response.
            // Codes_SRS_CSHARP_MQTT_TRANSPORT_18_006:  `SendMethodResponseAsync` shall set the message topic to '$iothub/methods/res/<STATUS>/?$rid=<REQUEST_ID>' where STATUS is the return status for the method and REQUEST_ID is the request ID received from the service in the original method call.
            // Codes_SRS_CSHARP_MQTT_TRANSPORT_18_007:  `SendMethodResponseAsync` shall set the message body to the response payload of the `Method` object.
            // Codes_SRS_CSHARP_MQTT_TRANSPORT_18_008:  `SendMethodResponseAsync` shall send the message to the service.
            var message = new Message(methodResponse.BodyStream);

            message.MqttTopicName = methodResponseTopic.FormatInvariant(methodResponse.Status, methodResponse.RequestId);

            await this.SendEventAsync(message, cancellationToken);
        }
Ejemplo n.º 7
0
 public override async Task SendMethodResponseAsync(MethodResponseInternal method, CancellationToken cancellationToken)
 {
     try
     {
         var sendState = new SendMessageState();
         await this.internalRetryPolicy.ExecuteAsync(() =>
         {
             return(base.SendMethodResponseAsync(method, cancellationToken));
         }, cancellationToken).ConfigureAwait(false);
     }
     catch (IotHubClientTransientException ex)
     {
         GetNormalizedIotHubException(ex).Throw();
     }
 }
Ejemplo n.º 8
0
        public override async Task SendMethodResponseAsync(MethodResponseInternal methodResponse, CancellationToken cancellationToken)
        {
            await this.HandleTimeoutCancellation(async() =>
            {
                Outcome outcome;
                using (AmqpMessage amqpMessage = methodResponse.ToAmqpMessage())
                {
                    outcome = await this.SendAmqpMethodResponseAsync(amqpMessage, cancellationToken);
                }

                if (outcome.DescriptorCode != Accepted.Code)
                {
                    throw AmqpErrorMapper.GetExceptionFromOutcome(outcome);
                }
            }, cancellationToken);
        }
Ejemplo n.º 9
0
        public async Task <AmqpIotOutcome> SendMethodResponseAsync(MethodResponseInternal methodResponse, TimeSpan timeout)
        {
            Logging.Enter(this, methodResponse, nameof(SendMethodResponseAsync));

            await EnableMethodsAsync(timeout).ConfigureAwait(false);

            Debug.Assert(_methodSendingLink != null);

            try
            {
                return(await _methodSendingLink.SendMethodResponseAsync(methodResponse, timeout).ConfigureAwait(false));
            }
            finally
            {
                Logging.Exit(this, methodResponse, nameof(SendMethodResponseAsync));
            }
        }
        public static AmqpMessage ConvertMethodResponseInternalToAmqpMessage(MethodResponseInternal methodResponseInternal, bool setBodyCalled = true)
        {
            methodResponseInternal.ThrowIfDisposed();

            AmqpMessage amqpMessage = null;

            if (methodResponseInternal.BodyStream == null)
            {
                amqpMessage = AmqpMessage.Create();
            }
            else
            {
                amqpMessage = AmqpMessage.Create(methodResponseInternal.BodyStream, false);
            }
            PopulateAmqpMessageFromMethodResponse(amqpMessage, methodResponseInternal);
            return(amqpMessage);
        }
Ejemplo n.º 11
0
 public override async Task SendMethodResponseAsync(MethodResponseInternal method, CancellationToken cancellationToken)
 {
     try
     {
         var sendState = new SendMessageState();
         await this.retryPolicy.ExecuteAsync(() =>
         {
             if (cancellationToken.IsCancellationRequested)
             {
                 return(Common.TaskConstants.Completed);
             }
             return(base.SendMethodResponseAsync(method, cancellationToken));
         }, cancellationToken);
     }
     catch (IotHubClientTransientException ex)
     {
         GetNormalizedIotHubException(ex).Throw();
     }
 }
Ejemplo n.º 12
0
        internal async Task <AmqpIotOutcome> SendMethodResponseAsync(MethodResponseInternal methodResponse, TimeSpan timeout)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, methodResponse, nameof(SendMethodResponseAsync));
            }

            using AmqpMessage amqpMessage = AmqpIotMessageConverter.ConvertMethodResponseInternalToAmqpMessage(methodResponse);
            AmqpIotMessageConverter.PopulateAmqpMessageFromMethodResponse(amqpMessage, methodResponse);

            Outcome outcome = await SendAmqpMessageAsync(amqpMessage, timeout).ConfigureAwait(false);

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

            return(new AmqpIotOutcome(outcome));
        }
Ejemplo n.º 13
0
        public override async Task SendMethodResponseAsync(MethodResponseInternal methodResponse, CancellationToken cancellationToken)
        {
            Logging.Enter(this, methodResponse, cancellationToken, $"{nameof(SendMethodResponseAsync)}");

            try
            {
                cancellationToken.ThrowIfCancellationRequested();
                AmqpIoTOutcome amqpIoTOutcome = await _amqpUnit.SendMethodResponseAsync(methodResponse, _operationTimeout).ConfigureAwait(false);

                if (amqpIoTOutcome != null)
                {
                    amqpIoTOutcome.ThrowIfNotAccepted();
                }
            }
            finally
            {
                Logging.Exit(this, methodResponse, cancellationToken, $"{nameof(SendMethodResponseAsync)}");
            }
        }
Ejemplo n.º 14
0
        public override async Task SendMethodResponseAsync(MethodResponseInternal methodResponse, CancellationToken cancellationToken)
        {
            try
            {
                if (Logging.IsEnabled)
                {
                    Logging.Enter(this, methodResponse, cancellationToken, $"{nameof(GateKeeperDelegatingHandler)}.{nameof(SendMethodResponseAsync)}");
                }

                await this.EnsureOpenedAsync(false, cancellationToken).ConfigureAwait(false);

                await base.SendMethodResponseAsync(methodResponse, cancellationToken).ConfigureAwait(false);
            }
            finally
            {
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, methodResponse, cancellationToken, $"{nameof(GateKeeperDelegatingHandler)}.{nameof(SendMethodResponseAsync)}");
                }
            }
        }
        internal async Task <AmqpIotOutcome> SendMethodResponseAsync(MethodResponseInternal methodResponse, CancellationToken cancellationToken)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, methodResponse, nameof(SendMethodResponseAsync));
            }

            cancellationToken.ThrowIfCancellationRequested();

            using AmqpMessage amqpMessage = AmqpIotMessageConverter.ConvertMethodResponseInternalToAmqpMessage(methodResponse);
            AmqpIotMessageConverter.PopulateAmqpMessageFromMethodResponse(amqpMessage, methodResponse);

            Outcome outcome = await SendAmqpMessageAsync(amqpMessage, cancellationToken).ConfigureAwait(false);

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

            return(new AmqpIotOutcome(outcome));
        }
        public async Task MqttTransportHandler_SendMethodResponseAsync_SendsMessage()
        {
            // arrange
            var            responseBytes = Encoding.UTF8.GetBytes(fakeMethodResponseBody);
            var            transport     = CreateTransportHandlerWithMockChannel(out IChannel channel);
            var            response      = new MethodResponseInternal(responseBytes, fakeResponseId, statusSuccess);
            MessageMatcher matches       = (msg) =>
            {
                return(StringComparer.InvariantCulture.Equals(msg.MqttTopicName, $"$iothub/methods/res/{statusSuccess}/?$rid={fakeResponseId}"));
            };

            // act
            transport.OnConnected();
            await transport.OpenAsync(CancellationToken.None).ConfigureAwait(false);

            await transport.SendMethodResponseAsync(response, CancellationToken.None).ConfigureAwait(false);

            // assert
            await channel
            .Received().WriteAndFlushAsync(Arg.Is <Message>(msg => matches(msg)))
            .ConfigureAwait(false);
        }
Ejemplo n.º 17
0
        public async Task <AmqpIotOutcome> SendMethodResponseAsync(MethodResponseInternal methodResponse, CancellationToken cancellationToken)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, methodResponse, nameof(SendMethodResponseAsync));
            }

            await EnableMethodsAsync(cancellationToken).ConfigureAwait(false);

            Debug.Assert(_methodSendingLink != null);

            try
            {
                return(await _methodSendingLink.SendMethodResponseAsync(methodResponse, cancellationToken).ConfigureAwait(false));
            }
            finally
            {
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, methodResponse, nameof(SendMethodResponseAsync));
                }
            }
        }
 public override Task SendMethodResponseAsync(MethodResponseInternal methodResponse, CancellationToken cancellationToken)
 {
     return(this.ExecuteWithErrorHandlingAsync(() => base.SendMethodResponseAsync(methodResponse, cancellationToken), true, cancellationToken));
 }
 public virtual Task SendMethodResponseAsync(MethodResponseInternal methodResponse, CancellationToken cancellationToken)
 {
     return(this.InnerHandler?.SendMethodResponseAsync(methodResponse, cancellationToken) ?? TaskConstants.Completed);
 }
Ejemplo n.º 20
0
        public override async Task SendMethodResponseAsync(MethodResponseInternal methodResponse, CancellationToken cancellationToken)
        {
            await this.EnsureOpenedAsync(false, cancellationToken);

            await base.SendMethodResponseAsync(methodResponse, cancellationToken);
        }
        /// <summary>
        /// Copies the Method instance's properties to the AmqpMessage instance.
        /// </summary>
        public static void PopulateAmqpMessageFromMethodResponse(AmqpMessage amqpMessage, MethodResponseInternal methodResponseInternal)
        {
            Fx.Assert(methodResponseInternal.RequestId != null, "Request Id is missing in the methodResponse.");

            amqpMessage.Properties.CorrelationId = new Guid(methodResponseInternal.RequestId);

            if (amqpMessage.ApplicationProperties == null)
            {
                amqpMessage.ApplicationProperties = new ApplicationProperties();
            }

            amqpMessage.ApplicationProperties.Map[Status] = methodResponseInternal.Status;
        }
Ejemplo n.º 22
0
 public virtual Task SendMethodResponseAsync(MethodResponseInternal methodResponse, CancellationToken cancellationToken)
 {
     ThrowIfDisposed();
     return(InnerHandler?.SendMethodResponseAsync(methodResponse, cancellationToken) ?? TaskHelpers.CompletedTask);
 }