Example #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;
        }
Example #2
0
        async Task 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;
#if !WINDOWS_UWP
                        case TransportType.Mqtt:
                            helper = new MqttTransportHandler(this.iotHubConnectionString, transportSetting as MqttTransportSettings);
                            break;
#endif
                        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 WINDOWS_UWP
                    // UWP does not use sockets
                    if (!(exception is IotHubCommunicationException || exception is TimeoutException || exception is AggregateException))
#else
                    if (!(exception is IotHubCommunicationException || exception is TimeoutException || exception is SocketException || exception is AggregateException))
#endif
                    {
                        throw;
                    }

                    if (exception is AggregateException)
                    {
                        var aggregateException = (AggregateException)exception;
                        var innerExceptions = aggregateException.Flatten().InnerExceptions;
#if WINDOWS_UWP
                        if (!innerExceptions.Any(x => x is IotHubCommunicationException || x is TimeoutException))
#else
                        if (!innerExceptions.Any(x => x is IotHubCommunicationException || x is SocketException || x is TimeoutException))
#endif
                        {
                            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;
        }