@@ -17,13 +17,10 @@ class Program
17
17
18
18
static void Main ( string [ ] args )
19
19
{
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
-
23
20
// Cert verification is not yet fully functional when using Windows OS for the container
24
21
bool bypassCertVerification = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ;
25
22
if ( ! bypassCertVerification ) InstallCert ( ) ;
26
- Init ( connectionString , bypassCertVerification ) . Wait ( ) ;
23
+ Init ( bypassCertVerification ) . Wait ( ) ;
27
24
28
25
// Wait until the app unloads or is cancelled
29
26
var cts = new CancellationTokenSource ( ) ;
@@ -72,10 +69,8 @@ static void InstallCert()
72
69
/// Initializes the DeviceClient and sets up the callback to receive
73
70
/// messages containing temperature information
74
71
/// </summary>
75
- static async Task Init ( string connectionString , bool bypassCertVerification = false )
72
+ static async Task Init ( bool bypassCertVerification = false )
76
73
{
77
- Console . WriteLine ( "Connection String {0}" , connectionString ) ;
78
-
79
74
MqttTransportSettings mqttSetting = new MqttTransportSettings ( TransportType . Mqtt_Tcp_Only ) ;
80
75
// During dev you might want to bypass the cert verification. It is highly recommended to verify certs systematically in production
81
76
if ( bypassCertVerification )
@@ -85,7 +80,7 @@ static async Task Init(string connectionString, bool bypassCertVerification = fa
85
80
ITransportSettings [ ] settings = { mqttSetting } ;
86
81
87
82
// Open a connection to the Edge runtime
88
- DeviceClient ioTHubModuleClient = DeviceClient . CreateFromConnectionString ( connectionString , settings ) ;
83
+ ModuleClient ioTHubModuleClient = await ModuleClient . CreateFromEnvironmentAsync ( settings ) ;
89
84
await ioTHubModuleClient . OpenAsync ( ) ;
90
85
Console . WriteLine ( "IoT Hub module client initialized." ) ;
91
86
@@ -102,8 +97,8 @@ static async Task<MessageResponse> PipeMessage(Message message, object userConte
102
97
{
103
98
int counterValue = Interlocked . Increment ( ref counter ) ;
104
99
105
- var deviceClient = userContext as DeviceClient ;
106
- if ( deviceClient == null )
100
+ var moduleClient = userContext as ModuleClient ;
101
+ if ( moduleClient == null )
107
102
{
108
103
throw new InvalidOperationException ( "UserContext doesn't contain " + "expected values" ) ;
109
104
}
@@ -119,7 +114,7 @@ static async Task<MessageResponse> PipeMessage(Message message, object userConte
119
114
{
120
115
pipeMessage . Properties . Add ( prop . Key , prop . Value ) ;
121
116
}
122
- await deviceClient . SendEventAsync ( "output1" , pipeMessage ) ;
117
+ await moduleClient . SendEventAsync ( "output1" , pipeMessage ) ;
123
118
Console . WriteLine ( "Received message sent" ) ;
124
119
}
125
120
return MessageResponse . Completed ;
0 commit comments