Skip to content

Commit 63b2744

Browse files
author
Summer
authored
update code template and sdk version (#1)
1 parent b1b5828 commit 63b2744

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
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>0.7.0</version>
5+
<version>0.8.0</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: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ namespace SampleModule
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Runtime.Loader;
7+
using System.Security.Cryptography.X509Certificates;
68
using System.Text;
79
using System.Threading;
810
using System.Threading.Tasks;
@@ -14,8 +16,12 @@ namespace SampleModule
1416
class Program
1517
{
1618
static int counter;
19+
1720
static void Main(string[] args)
1821
{
22+
// Install CA certificate
23+
InstallCert();
24+
1925
// Initialize Edge Module
2026
InitEdgeModule().Wait();
2127

@@ -25,6 +31,7 @@ static void Main(string[] args)
2531
Console.CancelKeyPress += (sender, cpe) => cts.Cancel();
2632
WhenCancelled(cts.Token).Wait();
2733
}
34+
2835
/// <summary>
2936
/// Handles cleanup operations when app is cancelled or unloads
3037
/// </summary>
@@ -35,27 +42,46 @@ public static Task WhenCancelled(CancellationToken cancellationToken)
3542
return tcs.Task;
3643
}
3744

45+
/// <summary>
46+
/// Add certificate in local cert store for use by client for secure connection to IoT Edge runtime
47+
/// </summary>
48+
static void InstallCert()
49+
{
50+
string certPath = Environment.GetEnvironmentVariable("EdgeModuleCACertificateFile");
51+
if (string.IsNullOrWhiteSpace(certPath))
52+
{
53+
// We cannot proceed further without a proper cert file
54+
Console.WriteLine("Missing path to certificate collection file.");
55+
throw new InvalidOperationException("Missing path to certificate file.");
56+
} else if (!File.Exists(certPath))
57+
{
58+
// We cannot proceed further without a proper cert file
59+
Console.WriteLine("Missing certificate collection file.");
60+
throw new InvalidOperationException("Missing certificate file.");
61+
}
62+
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
63+
store.Open(OpenFlags.ReadWrite);
64+
store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(certPath)));
65+
Console.WriteLine("Added Cert: " + certPath);
66+
store.Close();
67+
}
68+
69+
3870
/// <summary>
3971
/// Initializes the Azure IoT Client for the Edge Module
4072
/// </summary>
4173
static async Task InitEdgeModule()
4274
{
4375
try
4476
{
45-
// Open a connection to the Edge runtime using MQTT transport and
77+
// Open a connection to the Edge runtime using MQTT transport and
4678
// the connection string provided as an environment variable
47-
ITransportSettings[] settings =
48-
{
49-
new MqttTransportSettings(TransportType.Mqtt_Tcp_Only)
50-
{ RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true }
51-
};
52-
53-
DeviceClient IoTHubModuleClient = DeviceClient.CreateFromConnectionString(Environment.GetEnvironmentVariable("EdgeHubConnectionString"), settings);
54-
await IoTHubModuleClient.OpenAsync();
79+
DeviceClient ioTHubModuleClient = DeviceClient.CreateFromConnectionString(Environment.GetEnvironmentVariable("EdgeHubConnectionString"), TransportType.Mqtt_Tcp_Only);
80+
await ioTHubModuleClient.OpenAsync();
5581
Console.WriteLine("IoT Hub module client initialized.");
5682

5783
// Register callback to be called when a message is received by the module
58-
await IoTHubModuleClient.SetInputMessageHandlerAsync("input1", PipeMessage, IoTHubModuleClient);
84+
await ioTHubModuleClient.SetInputMessageHandlerAsync("input1", PipeMessage, ioTHubModuleClient);
5985

6086
}
6187
catch (AggregateException ex)

content/dotnet-template-azure-iot-edge-module/CSharp/SampleModule.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.6.0-preview-003" />
13+
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.6.0-preview-004" />
1414
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0-preview2-final" />
1515
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.0.0-preview2-final" />
1616
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.0.0-preview2-final" />

0 commit comments

Comments
 (0)