async AsyncTask TryOpenPrioritizedTransportsAsync()
        {
            Exception lastException = null;

            // Concrete Device Client creation was deferred. Use prioritized list of transports.
            foreach (var transportSetting in this.transportSettings)
            {
                TransportHandlerBase helper;
                try
                {
                    switch (transportSetting.GetTransportType())
                    {
                    case TransportType.Amqp_WebSocket_Only:
                    case TransportType.Amqp_Tcp_Only:
                        helper = new AmqpTransportHandler(this.iotHubConnectionString, transportSetting as AmqpTransportSettings);
                        break;

                    case TransportType.Http1:
                        helper = new HttpTransportHandler(this.iotHubConnectionString, transportSetting as Http1TransportSettings);
                        break;

                    case TransportType.Mqtt:
                        helper = new MqttTransportHandler(this.iotHubConnectionString, transportSetting as MqttTransportSettings);
                        break;

                    default:
                        throw new InvalidOperationException("Unsupported Transport Setting {0}".FormatInvariant(transportSetting));
                    }

                    // Try to open a connection with this transport
                    await helper.OpenAsync();
                }
                catch (Exception exception)
                {
                    if (exception.IsFatal() || exception is UnauthorizedException || exception is InvalidOperationException)
                    {
                        throw;
                    }

                    lastException = exception;

                    // open connection failed. Move to next transport type
                    continue;
                }

                // Success - return this transport type
                this.impl = helper;
                this.TransportTypeInUse = transportSetting.GetTransportType();

                return;
            }

            throw lastException;
        }
Exemple #2
0
        public void HttpTransportHandler_DisableReceiveMessageAsync_ThrowsNotSupportedException()
        {
            // arrange
            using var tokenSource = new CancellationTokenSource();
            HttpTransportHandler mockTransport = CreateFromConnectionString();

            // act
            Func <Task> act = async() =>
            {
                await mockTransport.DisableReceiveMessageAsync(tokenSource.Token).ConfigureAwait(false);
            };

            // assert
            act.Should().Throw <NotSupportedException>();
        }
Exemple #3
0
        /// <summary>
        /// Uploads a stream to a block blob in a storage account associated with the IoTHub for that device.
        /// If the blob already exists, it will be overwritten.
        /// </summary>
        /// <param name="blobName"></param>
        /// <param name="source"></param>
        /// <returns>AsncTask</returns>
        public AsyncTask UploadToBlobAsync(String blobName, System.IO.Stream source)
        {
            if (String.IsNullOrEmpty(blobName))
            {
                throw Fx.Exception.ArgumentNull("blobName");
            }
            if (source == null)
            {
                throw Fx.Exception.ArgumentNull("source");
            }

            var httpTransport = new HttpTransportHandler(iotHubConnectionString);

            return(httpTransport.UploadToBlobAsync(blobName, source));
        }
        /// <summary>
        /// Uploads a stream to a block blob in a storage account associated with the IoTHub for that device.
        /// If the blob already exists, it will be overwritten.
        /// </summary>
        /// <param name="blobName"></param>
        /// <param name="source"></param>
        /// <returns>AsncTask</returns>
        public AsyncTask UploadToBlobAsync(String blobName, System.IO.Stream source)
        {
            if (String.IsNullOrEmpty(blobName))
            {
                throw Fx.Exception.ArgumentNull("blobName");
            }
            if (source == null)
            {
                throw Fx.Exception.ArgumentNull("source");
            }
            if (blobName.Length > 1024)
            {
                throw Fx.Exception.Argument("blobName", "Length cannot exceed 1024 characters");
            }
            if (blobName.Split('/').Count() > 254)
            {
                throw Fx.Exception.Argument("blobName", "Path segment count cannot exceed 254");
            }

            var httpTransport = new HttpTransportHandler(iotHubConnectionString);

            return(httpTransport.UploadToBlobAsync(blobName, source));
        }
        async AsyncTask TryOpenPrioritizedTransportsAsync()
        {
            Exception lastException = null;

            // Concrete Device Client creation was deferred. Use prioritized list of transports.
            foreach (var transportSetting in this.transportSettings)
            {
                TransportHandlerBase helper = null;
                try
                {
                    switch (transportSetting.GetTransportType())
                    {
                    case TransportType.Amqp_WebSocket_Only:
                    case TransportType.Amqp_Tcp_Only:
                        helper = new AmqpTransportHandler(this.iotHubConnectionString, transportSetting as AmqpTransportSettings);
                        break;

                    case TransportType.Http1:
                        helper = new HttpTransportHandler(this.iotHubConnectionString, transportSetting as Http1TransportSettings);
                        break;

                    case TransportType.Mqtt:
                        helper = new MqttTransportHandler(this.iotHubConnectionString, transportSetting as MqttTransportSettings);
                        break;

                    default:
                        throw new InvalidOperationException("Unsupported Transport Setting {0}".FormatInvariant(transportSetting));
                    }

                    // Try to open a connection with this transport
                    await helper.OpenAsync();
                }
                catch (Exception exception)
                {
                    await helper.CloseAsync();

                    if (!(exception is IotHubCommunicationException || exception is TimeoutException || exception is SocketException || exception is AggregateException))
                    {
                        throw;
                    }

                    if (exception is AggregateException)
                    {
                        var aggregateException = (AggregateException)exception;
                        var innerExceptions    = aggregateException.Flatten().InnerExceptions;
                        if (!innerExceptions.Any(x => x is IotHubCommunicationException || x is SocketException || x is TimeoutException))
                        {
                            throw;
                        }
                    }

                    lastException = exception;

                    // open connection failed. Move to next transport type
                    continue;
                }

                // Success - return this transport type
                this.impl = helper;
                this.TransportTypeInUse = transportSetting.GetTransportType();

                return;
            }

            throw lastException;
        }
 public async Task HttpTransportHandler_RejectAsync_TokenCancellationRequested()
 {
     await TestOperationCanceledByToken(token => HttpTransportHandler.CreateFromConnectionString(DumpyConnectionString).RejectAsync(Guid.NewGuid().ToString(), token));
 }
 public async Task HttpTransportHandler_ReceiveAsync_TokenCancellationRequested()
 {
     await TestOperationCanceledByToken(token => HttpTransportHandler.CreateFromConnectionString(DumpyConnectionString).ReceiveAsync(new TimeSpan(0, 0, 0), token));
 }
 public async Task HttpTransportHandler_SendEventAsync_MultipleMessages_TokenCancellationRequested()
 {
     await TestOperationCanceledByToken(token => HttpTransportHandler.CreateFromConnectionString(DumpyConnectionString).SendEventAsync(new List <Message>(), token));
 }