I am currently upgrading NServiceBus from 2.x to 7.x. This has been quite the task, and I’m having a problem with my message handlers resolving the DbContext from my Autofac container. The DbContext in the message handler, is resolved through DI, and the actual save changes call is through a Unit of Work in a separate project. The unit of work is using the root scope while the Handler is using a different scope (where the data is manipulated).
In the past, we used Autofac2Builder to pass in a preconfigured container to be used by nServiceBus:
var config = NServiceBus.Configure.With(); config.Autofac2Builder(Container) .XmlSerializer() .MsmqSubscriptionStorage() .MsmqTransport() .IsTransactional(true) .PurgeOnStartup(false) .UnicastBus() .ImpersonateSender(true);
Then when a Message Handler used a Unit of Work (using statement) we would call a Container.Get() to resolve the DbContext which would first pull back the following scope.
// next look in NServiceBus context if (Autofac2ObjectBuilder.MessageLifetimeScope != null) return Autofac2ObjectBuilder.MessageLifetimeScope;
This allowed the DbContext in the handler and the unit of work to be the same.
This utilized NServiceBus.ObjectBuildre.Autofac2 which no longer exists. And normally when the Unit of Work in the Message Handler tried to resolve the context it used this scope.
Is it still possible to resolve the DbContext in a Message Handler, through DI, and in a unit of work (using statement) resolve the same DbContext as done previously using the Autofac2Builder.