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
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?
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?
It does 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.
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 ) providing a container implementation for the Microsoft.Extensions.DependencyInjection you need another container such as Autofac to bridge between.
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.
@twenzel ach… ok so this still has the same problem I was having with Autofac in that the services aren’t being updated. For example, services.AddSignalR registers IHubContext<T> which is needed if an NSB handler ever hopes to being to communicate with a user connected to the hub. The problem is that as user connect and disconnect from the hub, IHubContext gets updated by the framework. It would appear that those changes aren’t being picked up in NSB’s internal service collection, almost as if the service collection is being copied from IServiceCollection instead of adapted, if that makes sense. I haven’t actually looked at the code yet, but this is what I guess is happening.
The result is:
service starts up
The current state of IHubContext is registered with NSB
A user connects and IHubContext is updated with that connection info
A message is received by the NSB handler which requests IHubContext
The handler tries to send a message to the connected user but the user’s connection doesn’t exist on this instance of IHubContext
The user never receives the message
I believe this is fixable in some way as I was able to find a workaround with the Autofac container by actually replacing the default netcore service provider with Autofac.Extensions.DependencyInjection.ActofacServiceProvider.
I don’t know for sure, but I think what might be happening is that the Autofac extension might be wrapping service definitions in adapters instead of creating new service definitions for NSB so that any changes are made available. So if I’m correct about the implementation, then I think the fix would be to not copy service definitions but create adapters instead.
I was able to verify that this is not a problem with SignalR but rather a problem with how libraries are interfacing with NSB’s IoC container. Autofac is an ok workaround at the moment, but I’d REALLY rather not being dealing with THREE different IoC containers, you know?