Skip to content

Commit 94cf8f2

Browse files
committed
Change PostgresConnectionStringProvider to singleton
Switched IPostgresConnectionStringProvider registration from scoped to singleton in DI. This ensures a single instance is used throughout the application's lifetime instead of per scope.
1 parent 9bde273 commit 94cf8f2

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

Sources/EasyExtensions.EntityFrameworkCore.Npgsql/Extensions/ServiceCollectionExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static IServiceCollection AddPostgresDbContext<TContext>(
3838
PostgresOptionsBuilder contextFactory = new();
3939
setup?.Invoke(contextFactory);
4040

41-
services.AddScoped<IPostgresConnectionStringProvider>(sp =>
41+
services.AddSingleton<IPostgresConnectionStringProvider>(sp =>
4242
{
4343
var configuration = sp.GetRequiredService<IConfiguration>();
4444
return new PostgresConnectionStringProvider(configuration, contextFactory);
@@ -87,6 +87,11 @@ public static IServiceCollection AddPostgresDbContext<TContext, TImplementation>
8787
PostgresOptionsBuilder contextFactory = new();
8888
setup?.Invoke(contextFactory);
8989

90+
services.AddSingleton<IPostgresConnectionStringProvider>(sp =>
91+
{
92+
var configuration = sp.GetRequiredService<IConfiguration>();
93+
return new PostgresConnectionStringProvider(configuration, contextFactory);
94+
});
9095
services.AddDbContext<TContext, TImplementation>((sp, builder) =>
9196
{
9297
var conStringProvider = sp.GetRequiredService<IPostgresConnectionStringProvider>();

0 commit comments

Comments
 (0)