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; }
/// <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 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; }