Azure / azure-iot-sdk-csharp

A C# SDK for connecting devices to Microsoft Azure IoT services
Other
463 stars 493 forks source link

Latest device client does not connect over MQTT #3413

Closed WillooWisp closed 4 months ago

WillooWisp commented 8 months ago

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);
timtay-microsoft commented 8 months ago

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

WillooWisp commented 8 months ago

I'm using .NET 8 and Windows 11.

timtay-microsoft commented 8 months ago

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.

WillooWisp commented 8 months ago

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

timtay-microsoft commented 8 months ago

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

Clockwork-Muse commented 8 months ago

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.

rido-min commented 7 months ago

I believe this issue is related to the lack of .NET8 support in dotnetty https://github.com/Azure/DotNetty/issues/614

nivalxer commented 7 months ago

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. https://github.com/Azure/DotNetty/pull/616

jonmikeli commented 7 months ago

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

codeputer commented 5 months ago

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

jenscski commented 5 months ago

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" />
andyk-ms commented 4 months ago

Should be addressed in the new release: https://github.com/Azure/azure-iot-sdk-csharp/releases/tag/2024-03-28