Azure/azure-iot-sdk-csharp

Latest device client does not connect over MQTT

WillooWisp opened this issue · 12 comments

Upgrading from the release in 2023-05-01 with Microsoft.Azure.Devices.Client 1.42.0 to 2023-12-11 and Microsoft.Azure.Devices.Client 1.42.1 using MQTT, causes the device client to never connect and thus calls to SetReceiveMessageHandlerAsync fails. Downgrading again without any code changes whatsoever makes it work again as expected.

var transportType = TransportType.Mqtt_Tcp_Only;
string deviceConnectionString = $"HostName={iotHubUri};DeviceId={DeviceId};SharedAccessKey={primaryKey}";
var options = new ClientOptions
{
    SdkAssignsMessageId = SdkAssignsMessageId.WhenUnset,
};

var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString, transportType, options);

deviceClient.SetConnectionStatusChangesHandler(OnConnnectionStatusChanges);

await deviceClient.SetReceiveMessageHandlerAsync(OnMessageReceivedAsync, null);

Can you share some details about what .NET framework you are using in this application? .NET 6+? And what OS are you running here?

I'm using .NET 8 and Windows 11.

We don't currently support running this SDK on .NET 8, but we'll try to look into this when we get the chance. In the meantime, targeting .NET 7 should fix this issue for you.

Why do you think. NET 8 is the cause of this when the previous version works fine with. NET 8?

The only meaningful change from the previous SDK version is the upgrade we made to DotNetty from 0.7.4 to 0.7.5. Given that you are seeing this problem (only?) when using MQTT, that suggests to me that there may be some new compatibility issue between DotNetty and .NET 8 in DotNetty 0.7.5

I think I'm hitting this as well, running in a .Net8 Jammy devcontainer. I'm actually attempting to use mosquitto as a test MQTT broker, so that I don't need developers to register devices to an actual Azure IoT Hub and interfere with each other (among other benefits). Under .Net8 it would just... hang when attempting to connect, whereas on .Net7 it will connect and start passing messages.

I believe this issue is related to the lack of .NET8 support in dotnetty Azure/DotNetty#614

The reason is that the logic for the SSLStream read buffer has been changed in .NET 8. DotNetty needs to be updated accordingly. I have submitted a pull request (PR), but I am uncertain how long it will take to be merged.
Azure/DotNetty#616

Similar issues with DPS (provisioning) after the last upgrade.
Context:

  • .NET 8 (no way to go back to .NET 7, it is not LTS)
  • Security type: SAS
  • Enrollment type: Group

Still waiting for this to be fixed? Is this not a urgent and necessary to restore faith in latest Nuget packages?

I think this is fixed in DotNetty now, add direct references to DoNetty in your code to use latest

<PackageReference Include="DotNetty.Codecs.Mqtt" Version="0.7.6" />
<PackageReference Include="DotNetty.Handlers" Version="0.7.6" />