Skip to content

Commit 5eb9847

Browse files
author
Summer
authored
update template to not valid cert in windows and rename edge config t… (#4)
* update template to not valid cert in windows and rename edge config to deployment.json * upgrade to 1.0.0 for public * update readme
1 parent 4b8cce6 commit 5eb9847

File tree

6 files changed

+80
-115
lines changed

6 files changed

+80
-115
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.9.0</version>
5+
<version>1.0.0</version>
66
<title>Azure IoT Edge Module</title>
77
<authors>Microsoft</authors>
88
<owners>microsoft, nugetazureiotedge</owners>

README.md

Lines changed: 27 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
# dotnet-template-azure-iot-edge-module
2-
> dotnet template to do scaffolding tool for azure iot edge module development.
2+
> dotnet template to do scaffolding for Azure IoT Edge module development.
33
4-
This ReadMe consists of two parts:
5-
- Guidance of how to install the dotnet template step by step
6-
- Containerize the the custom module to docker
7-
8-
The dotnet template sets up all necessary files for you to focus on module functionality programming.
9-
10-
After the coding part completed, following the steps in this part to leverage docker to containerize your module so that they can be deployed and monitored by the new features of Azure IoT Edge more straight forward.
4+
This README will introduce how to install the dotnet template and then create Azure IoT Edge module with the template step by step.
5+
The template will set up all necessary files for you to focus on functionality programming.
116

127
## Get Started
138

14-
Make sure you have [.NET Core SDK](https://www.microsoft.com/net/core#windowscmd) and [Nuget](https://www.nuget.org/) installed.
9+
Make sure you have [.Net Core 2.0 SDK](https://www.microsoft.com/net/download/core) installed.
1510

16-
Run dotnet command to install the template:
11+
Run `dotnet` command to install the template:
1712

1813
```
1914
dotnet new -i Microsoft.Azure.IoT.Edge.Module
@@ -46,14 +41,13 @@ MVC ViewStart viewstart
4641

4742
Check out the template details:
4843
```
49-
PS C:\Users\qisun\Desktop> dotnet new aziotedgemodule --help
44+
PS C:\> dotnet new aziotedgemodule --help
5045
Usage: new [options]
5146
5247
Options:
5348
-h, --help Displays help for this command.
5449
-l, --list Lists templates containing the specified name. If no name is specified, lists all templates.
55-
-n, --name The name for the output being created. If no name is specified, the name of the current directory
56-
is used.
50+
-n, --name The name for the output being created. If no name is specified, the name of the current directory is used.
5751
-o, --output Location to place the generated output.
5852
-i, --install Installs a source or a template pack.
5953
-u, --uninstall Uninstalls a source or a template pack.
@@ -65,61 +59,33 @@ is used.
6559
Azure IoT Edge Module (C#)
6660
Author: Summer Sun
6761
Options:
68-
-s|--skipRestore
69-
bool - Optional
70-
Default: false
62+
-t|--target
63+
all
64+
deploy
65+
Default: all
7166
7267
-lx|--linux-x64
73-
bool - Optional
74-
Default: true
75-
```
76-
77-
Now create the azure iot edge module by the template with name:
78-
79-
```
80-
dotnet new aziotedgemodule -n <name>
81-
```
82-
83-
We support multiple architectures, but currently only linux-x64 is ready. So it is set the default.
84-
85-
## Containerize the custom module
86-
87-
azure-iot-edge-module sets up the Azure IoT Edge Module development environment, generating all necessary files for you.
88-
89-
To run the module in docker container, there are several steps to do.
68+
bool - Optional
69+
Default: true
9070
91-
### Install docker
92-
Ubuntu
71+
-wn|--windows-nano
72+
bool - Optional
73+
Default: true
9374
94-
https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/
95-
96-
Windows 10
97-
98-
https://download.docker.com/win/stable/InstallDocker.msi
99-
100-
MAC
101-
102-
https://store.docker.com/editions/community/docker-ce-desktop-mac
103-
104-
Now navigate to the generated module folder in the first place.
105-
106-
### Setup azure resources
107-
108-
If you have develop experience with Azure, you could skip this part and go ahead to next one.
109-
110-
1. Create an active Azure account
111-
112-
(If you don't have an account, you can create one [free account](http://azure.microsoft.com/pricing/free-trial/) in minutes.)
75+
-s|--skipRestore
76+
bool - Optional
77+
Default: false
11378
114-
2. Create an Azure IoT Hub
79+
```
11580

116-
Reference [How to create an azure iot hub] (https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-create-through-portal) for step by step guidance.
81+
Parameter `-t` means you if want all Azure IoT Edge module files or just a deployment.json file.
11782

118-
3. Create a device in azure iot hub
83+
Parameter `-lx` means you if want Dockerfile for linux-x64 or not. So does the `-wn` for windows-nano.
11984

120-
Navigate to your iot hub in azure portal, find the **Device Explorer** to **Add** a device in the portal.
121-
Mark up the device connection string after creating completed.
85+
Parameter `-s` means if you want to skip the restore of packages referenced in module project.
12286

123-
### Deploy the run the module
87+
Now create the Azure IoT Edge module by the template with name:
12488

125-
TBD
89+
```
90+
dotnet new aziotedgemodule -n <your_module_name>
91+
```

content/dotnet-template-azure-iot-edge-module/CSharp/.template.config/template.json

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@
1919
}
2020
],
2121
"symbols": {
22-
"skipRestore": {
22+
"target": {
2323
"type": "parameter",
24-
"datatype": "bool",
25-
"defaultValue": "false"
24+
"datatype": "choice",
25+
"defaultValue": "all",
26+
"choices": [
27+
{
28+
"choice": "all"
29+
},
30+
{
31+
"choice": "deploy"
32+
}
33+
]
2634
},
2735
"linux-x64": {
2836
"type": "parameter",
@@ -34,18 +42,10 @@
3442
"datatype": "bool",
3543
"defaultValue": "true"
3644
},
37-
"target": {
45+
"skipRestore": {
3846
"type": "parameter",
39-
"datatype": "choice",
40-
"defaultValue": "All",
41-
"choices": [
42-
{
43-
"choice": "All"
44-
},
45-
{
46-
"choice": "Config"
47-
}
48-
]
47+
"datatype": "bool",
48+
"defaultValue": "false"
4949
}
5050
},
5151
"sources": [
@@ -58,19 +58,19 @@
5858
],
5959
"modifiers": [
6060
{
61-
"condition": "(windows-nano && target != 'Config')",
61+
"condition": "(windows-nano && target != 'deploy')",
6262
"include": [
6363
"Docker/windows-nano/*"
6464
]
6565
},
6666
{
67-
"condition": "(linux-x64 && target != 'Config')",
67+
"condition": "(linux-x64 && target != 'deploy')",
6868
"include": [
6969
"Docker/linux-x64/*"
7070
]
7171
},
7272
{
73-
"condition": "(target == 'Config')",
73+
"condition": "(target == 'deploy')",
7474
"exclude": [
7575
"*.cs", "*.csproj", ".gitignore"
7676
]
@@ -80,11 +80,11 @@
8080
],
8181
"postActions": [
8282
{
83-
"condition": "(!skipRestore && target != 'Config')",
83+
"condition": "(!skipRestore && target != 'deploy')",
8484
"description": "Restore NuGet packages required by this project.",
8585
"manualInstructions": [
8686
{
87-
"text": "Run 'dotnet restore -s https://www.myget.org/F/aziot-device-sdk/api/v3/index.json'"
87+
"text": "Run 'dotnet restore'"
8888
}
8989
],
9090
"actionId": "210D431B-A78B-4D2F-B762-4ED3E3EA9025",

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

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
namespace SampleModule
22
{
33
using System;
4-
using System.Collections.Generic;
54
using System.IO;
5+
using System.Collections.Generic;
6+
using System.Runtime.InteropServices;
67
using System.Runtime.Loader;
78
using System.Security.Cryptography.X509Certificates;
89
using System.Text;
@@ -19,11 +20,10 @@ class Program
1920

2021
static void Main(string[] args)
2122
{
22-
// Install CA certificate
23+
// The Edge runtime gives us the connection string we need -- it is injected as an environment variable
24+
string connectionString = Environment.GetEnvironmentVariable("EdgeHubConnectionString");
2325
InstallCert();
24-
25-
// Initialize Edge Module
26-
InitEdgeModule().Wait();
26+
Init(connectionString).Wait();
2727

2828
// Wait until the app unloads or is cancelled
2929
var cts = new CancellationTokenSource();
@@ -47,16 +47,23 @@ public static Task WhenCancelled(CancellationToken cancellationToken)
4747
/// </summary>
4848
static void InstallCert()
4949
{
50+
// Suppress cert validation on Windows for now
51+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
52+
{
53+
return;
54+
}
55+
5056
string certPath = Environment.GetEnvironmentVariable("EdgeModuleCACertificateFile");
5157
if (string.IsNullOrWhiteSpace(certPath))
5258
{
5359
// We cannot proceed further without a proper cert file
54-
Console.WriteLine("Missing path to certificate collection file.");
60+
Console.WriteLine($"Missing path to certificate collection file: {certPath}");
5561
throw new InvalidOperationException("Missing path to certificate file.");
56-
} else if (!File.Exists(certPath))
62+
}
63+
else if (!File.Exists(certPath))
5764
{
5865
// We cannot proceed further without a proper cert file
59-
Console.WriteLine("Missing certificate collection file.");
66+
Console.WriteLine($"Missing path to certificate collection file: {certPath}");
6067
throw new InvalidOperationException("Missing certificate file.");
6168
}
6269
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
@@ -68,36 +75,28 @@ static void InstallCert()
6875

6976

7077
/// <summary>
71-
/// Initializes the Azure IoT Client for the Edge Module
78+
/// Initializes the DeviceClient and sets up the callback to receive
79+
/// messages containing temperature information
7280
/// </summary>
73-
static async Task InitEdgeModule()
81+
static async Task Init(string connectionString)
7482
{
75-
try
76-
{
77-
// Open a connection to the Edge runtime using MQTT transport and
78-
// the connection string provided as an environment variable
79-
DeviceClient ioTHubModuleClient = DeviceClient.CreateFromConnectionString(Environment.GetEnvironmentVariable("EdgeHubConnectionString"), TransportType.Mqtt_Tcp_Only);
80-
await ioTHubModuleClient.OpenAsync();
81-
Console.WriteLine("IoT Hub module client initialized.");
83+
Console.WriteLine("Connection String {0}", connectionString);
8284

83-
// Register callback to be called when a message is received by the module
84-
await ioTHubModuleClient.SetInputMessageHandlerAsync("input1", PipeMessage, ioTHubModuleClient);
85-
86-
}
87-
catch (AggregateException ex)
85+
MqttTransportSettings mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only);
86+
// Suppress cert validation on Windows for now
87+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
8888
{
89-
foreach (Exception exception in ex.InnerExceptions)
90-
{
91-
Console.WriteLine();
92-
Console.WriteLine("Error when initializing module: {0}", exception);
93-
}
94-
}
95-
catch (Exception ex)
96-
{
97-
Console.WriteLine();
98-
Console.WriteLine("Error when initializing module: {0}", ex.Message);
89+
mqttSetting.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
9990
}
91+
ITransportSettings[] settings = { mqttSetting };
92+
93+
// Open a connection to the Edge runtime
94+
DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(connectionString, settings);
95+
await deviceClient.OpenAsync();
96+
Console.WriteLine("IoT Hub module client initialized.");
10097

98+
// Register callback to be called when a message is received by the module
99+
await deviceClient.SetInputMessageHandlerAsync("input1", PipeMessage, deviceClient);
101100
}
102101

103102
/// <summary>
@@ -132,4 +131,4 @@ static async Task<MessageResponse> PipeMessage(Message message, object userConte
132131
return MessageResponse.Completed;
133132
}
134133
}
135-
}
134+
}

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-004" />
13+
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.6.0-preview-001" />
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" />
File renamed without changes.

0 commit comments

Comments
 (0)