dotnet / MQTTnet

MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
MIT License
4.44k stars 1.06k forks source link

Microsoft.Extensions.DependencyInjection 2.0.0 breaks MQTTnet #93

Closed ghost closed 6 years ago

ghost commented 6 years ago

Package dependencies specify Microsoft.Extensions.DependencyInjection v1.1.1 or greater. But updating to Microsoft.Extensions.DependencyInjection 2.0.0 breaks MQTTnet. This statement

        var mqttServer = new MqttFactory().CreateMqttServer();

reproduces it.

chkr1011 commented 6 years ago

Hi, thank you for sharing this issue. If you use the DI library in version 2.0.0 in your project please use the following code for constructing a MQTT server:

var services = new ServiceCollection()
    .AddMqttServer(options =>
    {
    // modify options here
    })
    .AddLogging()
    .BuildServiceProvider();

var mqttServer = services.GetRequiredService<IMqttServer>();

@JanEggers Do we havy any chance to fix this? I found several other tickets for other projects which have the same problem because 2.0.0 of DI has a breaking change (The service provider is no longer returned as an interface).

Best regards Christian

JanEggers commented 6 years ago

i will update myself then i will get that same error

chkr1011 commented 6 years ago

Here is an interesting post from people having the same problem: https://github.com/OData/WebApi/pull/1082

JanEggers commented 6 years ago

Do we havy any chance to fix this?

shure we can add netstadard2.0 build with updated dependencies. pr incoming

chkr1011 commented 6 years ago

Well for me this not a solution because (as also discussed at the other projects which are affected) this will require all library users to use .net standard 2.0 including ASP.NET Core 2.0 and .NET 4.6+. But my goal is to still support .NET 4.5.2 and to not rely on .net core (standard) 2.0 because it is just released and I will not limit the library to only be usable by cutting edge technologies only.

I am trying to use reflection for service lookup and using object as the type for the service provider field. I will let you know if this works.

JanEggers commented 6 years ago

no sry i didnt put it correctly we can add another targetplatform with updated dependencies but still maintain netstandard 1.3

JanEggers commented 6 years ago

im currently working on <TargetFrameworks>netstandard1.3;netstandard2.0;net452;uap10.0</TargetFrameworks>

chkr1011 commented 6 years ago

Ah OK sorry I don't get that. This is surely OK for me 😄

JanEggers commented 6 years ago

i also added a new event for server started so there are no messages published in aspnetcore sample until the server is started

danehnert commented 6 years ago

I'm currently using MQTTnet in a .NET Framework 4.5.2. When I now try to update the package from 2.4 to 2.5, it is showing me dependencies like Microsoft.Extensions.DependencyInjection. Aren't these dependencies specific for .NET Core?

JanEggers commented 6 years ago

@danehnert we use Microsoft.Extensions.DependencyInjection as ServiceProvider for netstandard and netfx

danehnert commented 6 years ago

So the library went from zero dependencies to this :-(

image

JanEggers commented 6 years ago

im not shure why visual studio picks dotnetstandard as targetframework instead of net452 which should result in less dependencies:

see https://github.com/chkr1011/MQTTnet/issues/80

@chkr1011 we have 2 little issues with the nuspec, can you please verify this should be 1.3: https://github.com/chkr1011/MQTTnet/blob/eaf27c1e1268a9417d96fd4d8925969aaaf19fcb/Build/MQTTnet.nuspec#L35

and this should be 452 not 451 https://github.com/chkr1011/MQTTnet/blob/eaf27c1e1268a9417d96fd4d8925969aaaf19fcb/Build/MQTTnet.nuspec#L52

chkr1011 commented 6 years ago

@danehnert These libraries are separate libraries if you use netstandard (v 1.6.1) in your screenshot. The "regular" .net Framework is a fat library with all libraries included. The new approach (decided by MS) is to split up things in separate libraries which can be maintained and released independently (most cases). So we have to accept this. Only the "Extensions" libraries are not part of the .net framework.

chkr1011 commented 6 years ago

@JanEggers Yes you are right. Then let us release a new version soon including the .net standard 2.0 fix.

JanEggers commented 6 years ago

agreed

chkr1011 commented 6 years ago

@JanEggers Yes they are OK.

JanEggers commented 6 years ago

il close this issue as 2.5.1 is released which also targets netstandard2.0 with updated dependencies. @danehnert im also investigating why there are more dlls added than needed that is tracked in #100 @ClaudioTF if you still have the issue with 2.5.1 feel free to reopen this issue

KarloX2 commented 6 years ago

Still I'm having this issue. I'm targeting .NET 4.6.1, using MQTTnet 2.5.1 and upgraded to Microsoft.Extensions.DependencyInjection 2.0.0 gives me an

System.MissingMethodException

on doing

var myclient = new MqttFactory().CreateMqttClient();

I'm really wondering why so many people are keen on DI. It never did anything useful for me. Only made things complicated, error prone and hard to understand (just my 2c).

BTW: same problem after changing code to

var svcprov = new ServiceCollection() .AddMqttClient() .BuildServiceProvider(); var myclient = svcprov.GetRequiredService<IMqttClient>();

schtibb commented 6 years ago

I'm having the same issue, I have reverted to version 2.4 in the meantime.

chkr1011 commented 6 years ago

This issue is fixed in the preview version 2.5.2-rc1 (available as nuget). Please let me know if the issue is now fixed.

KarloX2 commented 6 years ago

For me this works with 2.5.2-rc1. Thanks!