Beispiel #1
0
        /// <summary>
        /// Create a InternalClient from individual parameters
        /// </summary>
        /// <param name="hostname">The fully-qualified DNS hostname of IoT Hub</param>
        /// <param name="gatewayHostname">The fully-qualified DNS hostname of Gateway</param>
        /// <param name="authenticationMethod">The authentication method that is used</param>
        /// <param name="transportType">The transportType used (Http1, Amqp or Mqtt), <see cref="TransportType"/></param>
        /// <param name="options">The options that allow configuration of the device client instance during initialization.</param>
        /// <returns>InternalClient</returns>
        public static InternalClient Create(string hostname, string gatewayHostname, IAuthenticationMethod authenticationMethod, TransportType transportType, ClientOptions options = default)
        {
            if (hostname == null)
            {
                throw new ArgumentNullException(nameof(hostname));
            }

            if (authenticationMethod == null)
            {
                throw new ArgumentNullException(nameof(authenticationMethod));
            }

            IotHubConnectionStringBuilder connectionStringBuilder = IotHubConnectionStringBuilder.Create(hostname, gatewayHostname, authenticationMethod);

            if (authenticationMethod is DeviceAuthenticationWithX509Certificate)
            {
                if (connectionStringBuilder.Certificate == null)
                {
                    throw new ArgumentException("certificate must be present in DeviceAuthenticationWithX509Certificate");
                }

                // If the file upload transport settings hasn't been specified, we will create one using the client certificate on the connection string
                if (options?.FileUploadTransportSettings == null)
                {
                    var fileUploadTransportSettings = new Http1TransportSettings {
                        ClientCertificate = connectionStringBuilder.Certificate
                    };
                    if (options == null)
                    {
                        options = new ClientOptions {
                            FileUploadTransportSettings = fileUploadTransportSettings
                        };
                    }
                    else
                    {
                        options.FileUploadTransportSettings = fileUploadTransportSettings;
                    }
                }

                InternalClient dc = CreateFromConnectionString(connectionStringBuilder.ToString(), PopulateCertificateInTransportSettings(connectionStringBuilder, transportType), options);
                dc.Certificate = connectionStringBuilder.Certificate;

                return(dc);
            }

            return(CreateFromConnectionString(connectionStringBuilder.ToString(), authenticationMethod, transportType, null, options));
        }
 /// <summary>
 /// Ensures that the ClientOptions is configured and initialized.
 /// If a certificate is provided, the fileUploadTransportSettings will use it during initialization.
 /// </summary>
 private static void EnsureOptionsIsSetup(X509Certificate2 cert, ref ClientOptions options)
 {
     if (options?.FileUploadTransportSettings == null)
     {
         var fileUploadTransportSettings = new Http1TransportSettings {
             ClientCertificate = cert
         };
         if (options == null)
         {
             options = new ClientOptions {
                 FileUploadTransportSettings = fileUploadTransportSettings
             };
         }
         else
         {
             options.FileUploadTransportSettings = fileUploadTransportSettings;
         }
     }
 }
 public void TransportSettingsTest_TransportType_Http()
 {
     var transportSetting = new Http1TransportSettings();
     Assert.IsTrue(transportSetting.GetTransportType() == TransportType.Http1, "Should be TransportType.Http1");
 }
 internal HttpDeviceClient(IotHubConnectionString iotHubConnectionString, Http1TransportSettings transportSettings)
     : this(iotHubConnectionString)
 {
     this.transportSettings = transportSettings;
 }
 internal HttpDeviceClient(IotHubConnectionString iotHubConnectionString, Http1TransportSettings transportSettings)
     :this(iotHubConnectionString)
 {
     this.transportSettings = transportSettings;
 }