A simple MongoDB client dependency injection package that wraps up the MongoClient and MongoDatabase setup for .NET applications.
dotnet add package Xeofd.MongoDbExtension
Bind MongoDB settings under the MongoDb section in your configuration source (appsettings.json, environment variables, etc.):
{
"MongoDb": {
"ConnectionString": "mongodb://user@host:27017",
"DatabaseName": "myDatabase",
"Password": "optionalPasswordOverride"
}
}| Property | Description |
|---|---|
ConnectionString |
The base MongoDB connection string. |
DatabaseName |
The database to resolve when injecting IMongoDatabase. Falls back to defaultDatabase. |
Password |
Optional. When set, overrides the password embedded in the connection string. |
Register the MongoDB services on your IServiceCollection:
builder.Services.AddMongoDb(builder.Configuration);This injects:
IMongoClient— a singletonMongoClientinstance.IMongoDatabase— a transient database scoped to the configuredDatabaseName.
Inject them wherever needed:
public class MyRepository(IMongoDatabase database)
{
private readonly IMongoCollection<MyDoc> _collection = database.GetCollection<MyDoc>("myCollection");
public Task<List<MyDoc>> GetAllAsync() => _collection.Find(_ => true).ToListAsync();
}Use the optional configureSettings callback to tweak the MongoClientSettings before the client is built (e.g. timeouts, cluster configuration, authentication):
builder.Services.AddMongoDb(builder.Configuration, settings =>
{
settings.ConnectTimeout = TimeSpan.FromSeconds(10);
settings.SocketTimeout = TimeSpan.FromSeconds(30);
settings.MaxConnectionPoolSize = 200;
});MongoDB supports AWS IAM authentication via the MONGODB-AWS auth mechanism. There are a few ways to wire this up with AddMongoDb.
Add authMechanism=MONGODB-AWS to your connection string and let the MongoDB driver walk its built-in AWS credential provider chain (env vars, EC2/ECS instance metadata, profile, etc.):
{
"MongoDb": {
"ConnectionString": "mongodb://host:27017/?authMechanism=MONGODB-AWS&authSource=$external",
"DatabaseName": "myDatabase"
}
}No callback is required — register as usual:
builder.Services.AddMongoDb(builder.Configuration);Install the dedicated MongoDB.Driver.Authentication.AWS package, which registers an AWS credential provider with the driver. After setting up your AWS configuration (e.g. via the AWS SDK or your own AWS config extension), call AddAWSAuthentication() on the client settings inside the configureSettings callback:
using MongoDB.Driver;
using MongoDB.Driver.Authentication.AWS;
builder.Services.AddMongoDb(builder.Configuration, settings =>
{
settings.AddAWSAuthentication();
});The connection string still needs the AWS auth mechanism so the driver knows to use it:
{
"MongoDb": {
"ConnectionString": "mongodb://host:27017/?authMechanism=MONGODB-AWS&authSource=$external",
"DatabaseName": "myDatabase"
}
}If you'd rather pass AWS credentials in directly (e.g. from the AWS SDK or a named profile), configure AWS first, then hand the credentials to MongoClientSettings via the configureSettings callback:
using Amazon.Runtime;
using Amazon.Runtime.CredentialManagement;
using MongoDB.Driver;
// 1. Resolve AWS credentials via the AWS SDK (or your own AWS config extension)
var chain = new CredentialProfileStoreChain();
chain.TryGetAWSCredentials("my-profile", out var awsCredentials);
var creds = awsCredentials.GetCredentials();
// 2. Register MongoDb and inject the AWS credentials into the client settings
builder.Services.AddMongoDb(builder.Configuration, settings =>
{
settings.Credential = MongoCredential.CreateFromAwsCredentials(
accessKeyId: creds.AccessKey,
secretAccessKey: creds.SecretKey,
sessionToken: creds.Token);
});The connection string still needs
authMechanism=MONGODB-AWS&authSource=$externalfor the credential to be applied against the correct auth source.
See the repository for license information.