in iothub/device/src/ClientFactory.cs [83:155]
internal 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));
}
if (transportType != TransportType.Amqp_Tcp_Only
&& transportType != TransportType.Mqtt_Tcp_Only
&& authenticationMethod is DeviceAuthenticationWithX509Certificate certificate
&& certificate.ChainCertificates != null)
{
throw new ArgumentException("Certificate chains are only supported on Amqp_Tcp_Only and Mqtt_Tcp_Only");
}
if (!string.IsNullOrWhiteSpace(options?.ModelId)
&& transportType == TransportType.Http1)
{
throw new InvalidOperationException("Plug and Play is not supported over the HTTP transport.");
}
var connectionStringBuilder = IotHubConnectionStringBuilder.Create(hostname, gatewayHostname, authenticationMethod);
// Make sure client options is initialized with the correct transport setting.
EnsureOptionsIsSetup(connectionStringBuilder.Certificate, ref options);
if (authenticationMethod is DeviceAuthenticationWithX509Certificate)
{
if (connectionStringBuilder.Certificate == null)
{
throw new ArgumentException("No certificate was found. To use certificate authentication certificate must be present.");
}
InternalClient internalClient = CreateFromConnectionString(
connectionStringBuilder.ToString(),
authenticationMethod,
PopulateCertificateInTransportSettings(connectionStringBuilder, transportType),
null,
options);
internalClient.Certificate = connectionStringBuilder.Certificate;
// Install all the intermediate certificates in the chain if specified.
if (connectionStringBuilder.ChainCertificates != null)
{
try
{
CertificateInstaller.EnsureChainIsInstalled(connectionStringBuilder.ChainCertificates);
}
catch (Exception ex)
{
if (Logging.IsEnabled)
Logging.Error(null, $"{nameof(CertificateInstaller)} failed to read or write to cert store due to: {ex}");
throw new UnauthorizedException($"Failed to provide certificates in the chain - {ex.Message}", ex);
}
}
return internalClient;
}
return CreateFromConnectionString(connectionStringBuilder.ToString(), authenticationMethod, transportType, null, options);
}