Support for Microsoft.Extensions.DependencyInjection?

nservicebus
dependency-injection

(Jeremy) #1

I apologize if this has already been asked for; I didn’t find an existing thread…

Is there currently a library that will allow me to use Microsoft.Extensions.DependencyInjection (IServiceCollection and IServiceProvider) as a container? In the docs, I see other 3rd party libraries, but not this one. Any help is appreciated, thanks!

I’m currently using the 7.x beta with an aspnetcore app so prelease packages are ok for me


Dependency Injection with Microsoft.Extensions.DependencyInjection
(Jeremy) #2

In the meanwhile, I seem to have been able to use Autofac as a bridge between NSB and IServiceCollection


(Daniel Marbach) #3

Hi Jeremy

Not sure I entirely follow. It is possible to use IServiceCollection natively with NServiceBus. You don’t even need an external container. For example

Integrates NServiceBus and IMessageSession with IServiceCollection.

See also https://github.com/danielmarbach/service-fabric-webinar/blob/master/stateless-queues/Front_Stateless/Front_Stateless.cs#L30

We never specify a specific container. And the example showed above uses NSB v6. Did you have anything specific in mind that is not supported as of now?

Regards
Daniel


(Jeremy) #4

So, if I don’t specify a container, it seems that the message handlers don’t get injected with any services. The example only shows you accessing the service collection, and the full example shows registering IMessageSession with the service collection which I think is another matter. I’ve otherwise set my NSB installer up the exact same way. NSB doesn’t appear to be using IServiceProvider to inject any of the handlers (IHandleMessage). Is there something else I’m missing?


(Simon Cropp) #5

@jstafford it would seem this sample closely matches your scenario https://docs.particular.net/samples/dependency-injection/aspnetcore/


(Jeremy) #6

It does :slight_smile: That’s actually where I got the idea to use Autofac as mentioned in my second comment. Seems silly to bring that in for just this purpose, hence the question about whether or not there’s already an NServiceBus container library/extension that solves this directly. I tried implementing my own ContainerDefinition but wasn’t quite working. Some NSB classes weren’t Building correctly.


(Simon Cropp) #7

Interesting perspective. i will point some people with more DI knowledge towards this thread.


(Daniel Marbach) #8

Ah @jstafford I get what you are saying. I looked at the problem from the wrong angle. The downsides of the uniform container that we introduced is that as you rightfully stated we require the container implementation to enable injection into handlers. Since we are currently (maybe not yet :wink: ) providing a container implementation for the Microsoft.Extensions.DependencyInjection you need another container such as Autofac to bridge between.


(Jeremy) #9

I see… Ok I’ll just use the Autofac bridge for now then. Hopefully we’ll see NServiceBus.MicrosoftDI (or whatever) before too long :slight_smile:


(Hadi Eskandari) #10

@jstafford I have created an internal issue to get this done. At this point, I don’t have an ETA for it but can let you know once it gets prioritized.


(Jeremy) #11

That’s all I can ask for! Thanks!


(Jeremy) #12

Welp… it finally bit me. I’m playing with netcore2.1 release of SignalR and it would seem that the Autofac solution for copying registrations is not fully working. I can see that the IHubContext<T, K> registrations are in IServiceCollection but they’re not being copied over to the Autofac builder. No errors. No clue what’s up. Point is… a first class, supported IServiceCollection implementation would be swell.


(Toni Wenzel) #13

Please have a look at NServiceBus.MSDependencyInjection.


(Daniel Marbach) #14

Hi Toni

Great stuff!. Would you want to add this to our docs site?

If you need help to get it going feel free to ping me at daniel dot marbach at particular dot net

Regards
Daniel


(Jeremy) #15

Awesome! I just gave it a try in a test project and things seem to be in working order. Straight into production, what could go wrong amirite? :smiley:


(Toni Wenzel) #16

We already use it in production :wink:.


(Jeremy) #17

Bangarang. I’m all over this, thank you!


(Daniel Marbach) #18

For the reference, we recently added the extension to our docs site

https://docs.particular.net/nservicebus/dependency-injection/msdependencyinjection

Regards
Daniel