question

OJB1-4839 avatar image
0 Votes"
OJB1-4839 asked OJB1-4839 commented

Azure IOT Hub - Configure your devices from a back-end service Tutorial C# Example MISSING

I'm trying to work my way through the Azure IOT Hubs documentation and have come to a grinding halt as the tutorial for "Configure your devices from a back-end service" does not provide a solution for .NET, they only provide a tutorial that requires using Node.Js for the simulated devices.

Can someone please advise where I get a C# equivalent guide to the above?

The tutorial in question is here: https://docs.microsoft.com/en-us/azure/iot-hub/tutorial-device-twins


azure-iot-hubazure-iotazure-iot-sdk
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SandervandeVelde42 avatar image
0 Votes"
SandervandeVelde42 answered

Hello @OJB1-4839 ,

it seems you want to explore Cloud-To-Device (C2D) communication.

Azure IoT supports both D2C and C2D communication out-of-the-box with the Device SDKs for different development languages.

If you are interested in C#, check out this blog post.

There you found examples of both Desired properties and Direct methods, two popular but different ways to communicate.

Desired properties are values set in the Device Twin, a document in the IoT Hub where devices check for changes if they connect to the cloud. So this is great for not-always-connected devices.

The Direct Method needs a live connection. The advantage is that you can expect an actual answer from the device or a time-out.

It also demonstrated commands. These are 'function calls' like direct methods but the device can pick them up later if they are not connected yet. Hence, no result is returned.

If you are interested in more training, please check out the MS Learn IoT-related modules and learning paths.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

OJB1-4839 avatar image
0 Votes"
OJB1-4839 answered OJB1-4839 commented

Hi SandervandeVelde42,

Thank you for your reply, I will read your article with great interest.

I have successfully set the desired properties for a device twin from the service side, but what I'm really after is example code in how to read those desired properties from the IOT Device end.

Hopefully your article covers this but if not, would you have nay other suggestions?

I don't understand why Microsoft do not provide a C# example in their documentation, is there any logical reason for this? many thanks

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Hello @OJB1-4839 ,

There are many C# IoT samples at the Code Sample page.



What you have to do is just adding a handler method for incoming desired properties:

deviceClient.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertyChanged, deviceClient).ConfigureAwait(false);


That handler picks up changes:


0 Votes 0 ·
 private static async Task OnDesiredPropertyChanged(TwinCollection desiredProperties, object userContext)
 {
     Console.WriteLine("One or more device twin desired properties changed:");
     Console.WriteLine(JsonConvert.SerializeObject(desiredProperties));
     var client = userContext as DeviceClient;
     var reportedProperties = new TwinCollection
     {
         ["DateTimeLastDesiredPropertyChangeReceived"] = DateTime.Now
     };
     await client.UpdateReportedPropertiesAsync(reportedProperties).ConfigureAwait(false);
     Console.WriteLine("Sent current time as reported property to device twin");
 }

Please accept an answer if correct. Original posters help the community find answers faster by identifying the correct answer.

1 Vote 1 ·

Thank you so much for your help :)

0 Votes 0 ·