Azure / Azure/elastic-db-tools

Connection Leak with ASP.NET Core DI

Open
#159 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
147
Forks
108
PR merge metrics
No merged PRs in 30d

Description

```
services.AddTransient();
services.AddScoped();

services.AddScoped>(provider =>
provider.GetService().CreateShardContext());

services.AddIdentity()
.AddEntityFrameworkStores>()
.AddDefaultTokenProviders();
```
I am using a single tenant config where i need to route to a customer shard based on the request. The DbShardContextFactory does this with help from the HttpContextAccessor.

Because the factory creates the DbContext instead of the DI Container it appears to not be disposing correctly even though defined as Scoped. so the `.AddEntityFrameworkStores>()`
call i believe is the issue

What is the correct pattern here to use the standard ASP.NET Core service collection?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the DbShardContextFactory and the ConXDbContext registration shown in the issue, then inspect how AddEntityFrameworkStores resolves and scopes the context. Reproduce the registration in an ASP.NET Core request scope and check whether the factory-created context is disposed when the scope ends. Done means identifying a standard service-collection pattern that preserves shard selection and verified context disposal.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, csharp
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.