Search code examples
azureuwpmqttiotazure-iot-hub

IotHubClientTransientException: Transient error occured, please retry


I have an UWP app installed in an upboard that reads IotHub messages sended to that deviceID.

deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString, TransportType.Mqtt);
Message receivedMessage = await deviceClient.ReceiveAsync();

The app works fine and reads the messages correctly, but sometimes I have these exceptions:

IotHubClientTransientException: Transient error occured, please retry.

I read that these errors may can be generated from wrong connection string, but it's not possible in my case.

Can someone help me?


Solution

  • The error is most likely caused by a network connectivity error. Just add a retry strategy. You could simply write your own or use a library like Polly.Net

    In a distributed world connectivity issues should be expected, so I don't think there is any problem with your code other than is should be more resilient. I think it is really nice that the exceptions even tells you it should be retried, most of the times you have to figure that out yourself.

    Some more guidance from the Azure team can be found here. In your case the Retry pattern is a good fit:

    Retry

    Enable an application to handle anticipated, temporary failures when it tries to connect to a service or network resource by transparently retrying an operation that's previously failed.