NServiceBus 5 Autofac Dependency Injection

(Marco Magnani) #1


I’m using NServiceBus 5 with Autofac. As suggested in the documentation I’ve configured the endpoint in this way (and it works):

var builder = new ContainerBuilder();
builder.RegisterInstance(new MyService());

.Register(x => Bus.Create(busConfiguration).Start())

customizations: customizations =>
// Child container needed as otherwise creation will hang
var childContainer = container.BeginLifetimeScope();

My question is: who will dispose the IDisposable childContainer?


(William Brander) #2

Hi Marco

With the code you’ve linked above, a child container will be created and that container will then be used by the Bus.

Because you are passing an existing instance of a container through (in this case, the child container) NServiceBus takes no responsibility for disposing of that instance as it could be used elsewhere. So to clean the resources up the Bus must first be Disposed, and then the child container must be disposed.

You can find more details on the V5 cleanup process here.