@@ -22,8 +22,11 @@ static void Main(string[] args)
22
22
{
23
23
// The Edge runtime gives us the connection string we need -- it is injected as an environment variable
24
24
string connectionString = Environment . GetEnvironmentVariable ( "EdgeHubConnectionString" ) ;
25
- InstallCert ( ) ;
26
- Init ( connectionString ) . Wait ( ) ;
25
+
26
+ // Cert verification is not yet fully functional when using Windows OS for the container
27
+ bool bypassCertVerification = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ;
28
+ if ( ! bypassCertVerification ) InstallCert ( ) ;
29
+ Init ( connectionString , bypassCertVerification ) . Wait ( ) ;
27
30
28
31
// Wait until the app unloads or is cancelled
29
32
var cts = new CancellationTokenSource ( ) ;
@@ -47,12 +50,6 @@ public static Task WhenCancelled(CancellationToken cancellationToken)
47
50
/// </summary>
48
51
static void InstallCert ( )
49
52
{
50
- // Suppress cert validation on Windows for now
51
- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
52
- {
53
- return ;
54
- }
55
-
56
53
string certPath = Environment . GetEnvironmentVariable ( "EdgeModuleCACertificateFile" ) ;
57
54
if ( string . IsNullOrWhiteSpace ( certPath ) )
58
55
{
@@ -78,25 +75,25 @@ static void InstallCert()
78
75
/// Initializes the DeviceClient and sets up the callback to receive
79
76
/// messages containing temperature information
80
77
/// </summary>
81
- static async Task Init ( string connectionString )
78
+ static async Task Init ( string connectionString , bool bypassCertVerification = false )
82
79
{
83
80
Console . WriteLine ( "Connection String {0}" , connectionString ) ;
84
81
85
82
MqttTransportSettings mqttSetting = new MqttTransportSettings ( TransportType . Mqtt_Tcp_Only ) ;
86
- // Suppress cert validation on Windows for now
87
- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
83
+ // During dev you might want to bypass the cert verification. It is highly recommended to verify certs systematically in production
84
+ if ( bypassCertVerification )
88
85
{
89
86
mqttSetting . RemoteCertificateValidationCallback = ( sender , certificate , chain , sslPolicyErrors ) => true ;
90
87
}
91
88
ITransportSettings [ ] settings = { mqttSetting } ;
92
89
93
90
// Open a connection to the Edge runtime
94
- DeviceClient deviceClient = DeviceClient . CreateFromConnectionString ( connectionString , settings ) ;
95
- await deviceClient . OpenAsync ( ) ;
91
+ DeviceClient ioTHubModuleClient = DeviceClient . CreateFromConnectionString ( connectionString , settings ) ;
92
+ await ioTHubModuleClient . OpenAsync ( ) ;
96
93
Console . WriteLine ( "IoT Hub module client initialized." ) ;
97
94
98
95
// Register callback to be called when a message is received by the module
99
- await deviceClient . SetInputMessageHandlerAsync ( "input1" , PipeMessage , deviceClient ) ;
96
+ await ioTHubModuleClient . SetInputMessageHandlerAsync ( "input1" , PipeMessage , ioTHubModuleClient ) ;
100
97
}
101
98
102
99
/// <summary>
0 commit comments