Skip to content

Commit 7ff52b6

Browse files
author
Summer
authored
update template due to windows cert issue: tutorial and code not match (#5)
* update template due to windows cert issue: tutorial and code not match * align with latest template * upgrade
1 parent 5eb9847 commit 7ff52b6

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

Microsoft.Azure.IoT.Edge.Module.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package>
33
<metadata>
44
<id>Microsoft.Azure.IoT.Edge.Module</id>
5-
<version>1.0.0</version>
5+
<version>1.0.1</version>
66
<title>Azure IoT Edge Module</title>
77
<authors>Microsoft</authors>
88
<owners>microsoft, nugetazureiotedge</owners>

content/dotnet-template-azure-iot-edge-module/CSharp/Program.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ static void Main(string[] args)
2222
{
2323
// The Edge runtime gives us the connection string we need -- it is injected as an environment variable
2424
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();
2730

2831
// Wait until the app unloads or is cancelled
2932
var cts = new CancellationTokenSource();
@@ -47,12 +50,6 @@ public static Task WhenCancelled(CancellationToken cancellationToken)
4750
/// </summary>
4851
static void InstallCert()
4952
{
50-
// Suppress cert validation on Windows for now
51-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
52-
{
53-
return;
54-
}
55-
5653
string certPath = Environment.GetEnvironmentVariable("EdgeModuleCACertificateFile");
5754
if (string.IsNullOrWhiteSpace(certPath))
5855
{
@@ -78,25 +75,25 @@ static void InstallCert()
7875
/// Initializes the DeviceClient and sets up the callback to receive
7976
/// messages containing temperature information
8077
/// </summary>
81-
static async Task Init(string connectionString)
78+
static async Task Init(string connectionString, bool bypassCertVerification = false)
8279
{
8380
Console.WriteLine("Connection String {0}", connectionString);
8481

8582
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)
8885
{
8986
mqttSetting.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
9087
}
9188
ITransportSettings[] settings = { mqttSetting };
9289

9390
// 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();
9693
Console.WriteLine("IoT Hub module client initialized.");
9794

9895
// 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);
10097
}
10198

10299
/// <summary>

0 commit comments

Comments
 (0)