[DeviceClient IoT Hub] Exception handling in callback

Andirly 16 Reputation points
2022-09-01T20:56:44.797+00:00

Hello,

I'm trying hard to figure out a way to do proper exception handling using the DeviceClient API to connect to IoT hub.

I've been using the API for a while and still I can't get my head around the design of the callback implementation

E.g.

public void SetConnectionStatusChangesHandler(ConnectionStatusChangesHandler statusChangesHandler);  

forces me to not use any async method inside the handler as I can't propagate any errors outside. A typical use case is fetching all upstream desired properties and upload reported properties after having checked for changes. But any error occuring during these functions (which are all async and therefore force me to have an async void callback) won't be fetched and basically crashes my application. All I can do is logging but that's all. What is the idea behind this design? Is there a workaround?

Thank you
Andy

Azure Digital Twins
Azure Digital Twins
An Azure platform that is used to create digital representations of real-world things, places, business processes, and people.
220 questions
Azure IoT Plug and Play
Azure IoT Plug and Play
A Microsoft technology based on an open modeling language that enables developers to connect internet of things (IoT) devices to the cloud without having to write any code.
15 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,128 questions
{count} votes

1 answer

Sort by: Most helpful
  1. David R. Williamson [MSFT] 26 Reputation points Microsoft Employee
    2022-09-06T15:48:16.237+00:00

    Even though the signature returns void, the method can still be declared as 'async'. Note that 'async' is not part of a method definition.

    Although async void methods are generally discouraged, they are common with callbacks where the caller (in this case, the SDK) does not await the call.

    1 person found this answer helpful.