Esempio n. 1
0
        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;
        }
Esempio n. 2
0
 DeviceClient(TransportHandlerBase impl, TransportType transportType)
 {
     this.impl = impl;
     this.TransportTypeInUse = transportType;
 }
Esempio n. 3
0
        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;
        }