Skip to content

Commit f6c1d24

Browse files
Update with new module client (#32)
1 parent b4de812 commit f6c1d24

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@ class Program
1717

1818
static void Main(string[] args)
1919
{
20-
// The Edge runtime gives us the connection string we need -- it is injected as an environment variable
21-
string connectionString = Environment.GetEnvironmentVariable("EdgeHubConnectionString");
22-
2320
// Cert verification is not yet fully functional when using Windows OS for the container
2421
bool bypassCertVerification = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
2522
if (!bypassCertVerification) InstallCert();
26-
Init(connectionString, bypassCertVerification).Wait();
23+
Init(bypassCertVerification).Wait();
2724

2825
// Wait until the app unloads or is cancelled
2926
var cts = new CancellationTokenSource();
@@ -72,10 +69,8 @@ static void InstallCert()
7269
/// Initializes the DeviceClient and sets up the callback to receive
7370
/// messages containing temperature information
7471
/// </summary>
75-
static async Task Init(string connectionString, bool bypassCertVerification = false)
72+
static async Task Init(bool bypassCertVerification = false)
7673
{
77-
Console.WriteLine("Connection String {0}", connectionString);
78-
7974
MqttTransportSettings mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only);
8075
// During dev you might want to bypass the cert verification. It is highly recommended to verify certs systematically in production
8176
if (bypassCertVerification)
@@ -85,7 +80,7 @@ static async Task Init(string connectionString, bool bypassCertVerification = fa
8580
ITransportSettings[] settings = { mqttSetting };
8681

8782
// Open a connection to the Edge runtime
88-
DeviceClient ioTHubModuleClient = DeviceClient.CreateFromConnectionString(connectionString, settings);
83+
ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);
8984
await ioTHubModuleClient.OpenAsync();
9085
Console.WriteLine("IoT Hub module client initialized.");
9186

@@ -102,8 +97,8 @@ static async Task<MessageResponse> PipeMessage(Message message, object userConte
10297
{
10398
int counterValue = Interlocked.Increment(ref counter);
10499

105-
var deviceClient = userContext as DeviceClient;
106-
if (deviceClient == null)
100+
var moduleClient = userContext as ModuleClient;
101+
if (moduleClient == null)
107102
{
108103
throw new InvalidOperationException("UserContext doesn't contain " + "expected values");
109104
}
@@ -119,7 +114,7 @@ static async Task<MessageResponse> PipeMessage(Message message, object userConte
119114
{
120115
pipeMessage.Properties.Add(prop.Key, prop.Value);
121116
}
122-
await deviceClient.SendEventAsync("output1", pipeMessage);
117+
await moduleClient.SendEventAsync("output1", pipeMessage);
123118
Console.WriteLine("Received message sent");
124119
}
125120
return MessageResponse.Completed;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netcoreapp2.0|AnyCPU'">
88
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
99
<TreatSpecificWarningsAsErrors />
10+
<RestoreSources>https://api.nuget.org/v3/index.json;https://www.myget.org/F/aziot-device-sdk/api/v3/index.json</RestoreSources>
1011
</PropertyGroup>
1112

1213
<ItemGroup>
13-
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.6.0-preview-001" />
14+
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.17.0-preview-009" />
1415
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0-preview2-final" />
1516
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.0.0-preview2-final" />
1617
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.0.0-preview2-final" />

0 commit comments

Comments
 (0)