1
- namespace SampleModule
2
- {
3
- using Microsoft . Azure . Devices . Client ;
4
- using Microsoft . Azure . Devices . Client . Transport . Mqtt ;
5
- using System ;
6
- using System . Runtime . Loader ;
7
- using System . Text ;
8
- using System . Threading ;
9
- using System . Threading . Tasks ;
1
+ using SampleModule ;
10
2
11
- class Program
12
- {
13
- static int counter ;
3
+ IHost host = Host . CreateDefaultBuilder ( args )
4
+ . ConfigureServices ( services => services . AddHostedService < ModuleBackgroundService > ( ) )
5
+ . Build ( ) ;
14
6
15
- static void Main ( )
16
- {
17
- Init ( ) . Wait ( ) ;
18
-
19
- // Wait until the app unloads or is cancelled
20
- var cts = new CancellationTokenSource ( ) ;
21
- AssemblyLoadContext . Default . Unloading += ( ctx ) => cts . Cancel ( ) ;
22
- Console . CancelKeyPress += ( sender , cpe ) => cts . Cancel ( ) ;
23
- WhenCancelled ( cts . Token ) . Wait ( ) ;
24
- }
25
-
26
- /// <summary>
27
- /// Handles cleanup operations when app is cancelled or unloads
28
- /// </summary>
29
- public static Task WhenCancelled ( CancellationToken cancellationToken )
30
- {
31
- var tcs = new TaskCompletionSource < bool > ( ) ;
32
- cancellationToken . Register ( s => ( ( TaskCompletionSource < bool > ) s ) . SetResult ( true ) , tcs ) ;
33
- return tcs . Task ;
34
- }
35
-
36
- /// <summary>
37
- /// Initializes the ModuleClient and sets up the callback to receive
38
- /// messages containing temperature information
39
- /// </summary>
40
- static async Task Init ( )
41
- {
42
- MqttTransportSettings mqttSetting = new ( TransportType . Mqtt_Tcp_Only ) ;
43
- ITransportSettings [ ] settings = { mqttSetting } ;
44
-
45
- // Open a connection to the Edge runtime
46
- ModuleClient ioTHubModuleClient = await ModuleClient . CreateFromEnvironmentAsync ( settings ) ;
47
- await ioTHubModuleClient . OpenAsync ( ) ;
48
- Console . WriteLine ( "IoT Hub module client initialized." ) ;
49
-
50
- // Register callback to be called when a message is received by the module
51
- await ioTHubModuleClient . SetInputMessageHandlerAsync ( "input1" , PipeMessage , ioTHubModuleClient ) ;
52
- }
53
-
54
- /// <summary>
55
- /// This method is called whenever the module is sent a message from the EdgeHub.
56
- /// It just pipe the messages without any change.
57
- /// It prints all the incoming messages.
58
- /// </summary>
59
- static async Task < MessageResponse > PipeMessage ( Message message , object userContext )
60
- {
61
- int counterValue = Interlocked . Increment ( ref counter ) ;
62
-
63
- if ( userContext is not ModuleClient moduleClient )
64
- {
65
- throw new InvalidOperationException ( "UserContext doesn't contain " + "expected values" ) ;
66
- }
67
-
68
- byte [ ] messageBytes = message . GetBytes ( ) ;
69
- string messageString = Encoding . UTF8 . GetString ( messageBytes ) ;
70
- Console . WriteLine ( $ "Received message: { counterValue } , Body: [{ messageString } ]") ;
71
-
72
- if ( ! string . IsNullOrEmpty ( messageString ) )
73
- {
74
- using var pipeMessage = new Message ( messageBytes ) ;
75
- foreach ( var prop in message . Properties )
76
- {
77
- pipeMessage . Properties . Add ( prop . Key , prop . Value ) ;
78
- }
79
- await moduleClient . SendEventAsync ( "output1" , pipeMessage ) ;
80
-
81
- Console . WriteLine ( "Received message sent" ) ;
82
- }
83
- return MessageResponse . Completed ;
84
- }
85
- }
86
- }
7
+ host . Run ( ) ;
0 commit comments