eclipse / paho.mqtt.m2mqtt

Eclipse Public License 1.0
512 stars 303 forks source link

Error connecting to AWS IoT Broker with C# client when deployed as Azure function #99

Open edcline opened 5 years ago

edcline commented 5 years ago

Using MQTT Client to read IoT data from a device through AWS IoT MQTT broker. Application is C# Azure Function App. The code works properly when tested under Azure function local machine test environment and also if compiled to a standalone command line VS project. However, when deployed to Azure Cloud an exception is thrown: "Exception connecting to the broker". The same cert files are used in all three scenarios. Is there a way to get more information about what the exception is? I'm assuming it is related to authentication, but not sure.

Code is below: ` string iotendpoint = "------------.iot.us-east-1.amazonaws.com"; int BrokerPort = 8883; string ClientId = Guid.NewGuid().ToString();

        // create client instance - LOCAL AZURE DEV
        //StaticVars.td.IoTClient = new MqttClient(iotendpoint, BrokerPort, true,
        //                        new X509Certificate2(@"C:\dev\projects\VisualStudio\caapi\caapi\Certs\rootCA.pfx", "123456"),
        //                        new X509Certificate2(@"C:\dev\projects\VisualStudio\caapi\caapi\Certs\clientCert.pfx", "123456"),
        //                        MqttSslProtocols.TLSv1_2);

        // create client instance - AZURE FUNCTION DEPLOYMENT
        StaticVars.td.IoTClient = new MqttClient(iotendpoint, BrokerPort, true,
                                new X509Certificate2(@"D:\home\site\wwwroot\Certs\rootCA.pfx", "123456"),
                                new X509Certificate2(@"D:\home\site\wwwroot\Certs\clientCert.pfx", "123456"),
                                MqttSslProtocols.TLSv1_2);

        // Register to message received event
        StaticVars.td.IoTClient.MqttMsgPublishReceived += Client_recievedMessage;
        byte cx = StaticVars.td.IoTClient.Connect(ClientId);
        log.Info($"Connected to MQTT Broker.  Return code: {cx}");`

Have looked at https://github.com/eclipse/paho.mqtt.m2mqtt/issues/80 and created rootCA.pfx as suggested in that example (which works fine in Azure local dev, but not in Azure cloud deployment).

Any suggestions are greatly appreciated.