Search code examples
azureasynchronousazure-iot-sdk

azure iothub device stops sending messages


I've recently started working with the Azure-Iot-SDK and wonder how to investigate this behaviour. My application is ok with sending messages to the IoT hub for some time, but suddenly just stops sending. Without an error or anything else that helps me find the rootcause for this. Just restarting the application (a service to be precise) is enough to get it working again.

Code is like this (on DataChangedEvent) :

try {    
    deviceClient = DeviceClient.Create(connectionString, x509Certificate, tType);
    Log("Start sending message")
    await deviceClient.SendEventAsync(message);
    DoLogging("Done sending!");
}
catch (Exception e)
{
    moreLogging("Error occurred");
}

Somehow the "Done sending!" message stops appearing in the logs, but the "Start sending message" keeps coming. Does anyone have any suggestions how to proceed?


Solution

  • From SendEventAsync method implementation,

    The async operation shall retry until time specified in OperationTimeoutInMilliseconds property expire or unrecoverable error(authentication or quota exceed) occurs.

    So check the OperationTimeoutInMilliseconds value to see if there is an error occurs after reach the timeout.

    Meanwhile, you can monitor the connection status by registering such handler:

    deviceClient.SetConnectionStatusChangesHandler(ConnectionStatusChangeHandler);
    

    The handler may look like this:

                    static void ConnectionStatusChangeHandler(ConnectionStatus status, ConnectionStatusChangeReason reason)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Connection Status Changed to {0}", status);
                        Console.WriteLine("Connection Status Changed Reason is {0}", reason);
                        Console.WriteLine();
                    }
    

    And you can try this way to see if it helps:

       await deviceClient.SendEventAsync(eventMessage).ConfigureAwait(false);