Can we use the dependency-injection container to resolve the MongoClient dependency within the NServiceBus MongoDB persistence module?

We are utilizing the MongoDB Persistence module with NServiceBus, and our company has utility library for MongoDB that creates and configures MongoClient instances that are configured in a common way and address some cross-cutting concerns.

We would like to have the MongoDB Persistence module utilize a MongoClient that is created by our utility library. The utility library is managing the MongoClient objects within ASP.NET Core dependency-injection container. At the point that we can configure the MongoDB Persistence module (IHostBuilder.UseNServiceBus) there is not an IServiceCollection or IServiceProvider that we can hook into to resolve a MongoClient from the dependency-injection container.

MongoDB Persistence • Particular Docs

I’m looking for ideas of how we could accomplish this, or perhaps the persistence module could be enhanced to rely on the dependency-injection container to resolve dependencies such as the MongoClient instance?

Thanks!

Hi Mike!

Unfortunately resolving the MongoDB client from DI isn’t supported yet. We are moving in this direction (our Azure table storage persisters already support this) and I have raised Support resolving the MongoDB client from DI · Issue #328 · Particular/NServiceBus.Storage.MongoDB · GitHub to track this. Feel free to subscribe if you want to get notified when we have addressed this.

For now, you would have to share the built client statically somehow, is this a blocker for your project?

Cheers,

Andreas

Hi Andreas,

Thanks for the quick reply on this! It is not a blocker for our project – more of a technical debt item. I will subscribe to the GitHub issue for future updates.

Best Regards,
Mike