[CSHARP-3470] Implement ASP.NET Core service collection extension method for client registration Created: 10/Mar/21  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Configuration
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Mikalai Mazurenka (Inactive) Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: FY22Q2
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

ASP.NET Core DI functions via IServiceCollection dependency registration.
While MongoClient can be registered with a general approach, like:

services.AddSingleton<IMongoClient>(_ => new MongoClient(connectionString));

There's a trend among prominent dotnet libraries in providing specialized extension methods for dependency registration, like:

services.AddMongoClient(connectionString);

This could be achieved by creating a separate nuget package (e.g. MongoDB.Extensions.DependencyInjection) with an extension implementation. The basic one is:

public static class ServiceCollectionExtensions
{
	public static IServiceCollection AddMongoClient(this IServiceCollection services, string connectionString)
	{
		services.AddSingleton<IMongoClient>(_ => new MongoClient(connectionString));
		return services;
	}
}

The major rational for this improvement is directing driver users towards using MongoClient as singleton by default, since not everyone notice the advised singleton lifetime scope in documentation, and per-instance usage sometimes leads to issues with the resource management.


Generated at Wed Feb 07 21:45:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.