I am using the ASP.NET Identity API in ASP.NET 8, using the following code in Program.cs:

var services = builder.Services;
services.AddDbContext<MyDbContext>();
services.AddDefaultIdentity<ApplicationUser>(options =>
{
    options.SignIn.RequireConfirmedAccount = false;
}).AddRoles<IdentityRole>().AddEntityFrameworkStores<MyDbContext>();

With this code, ASP.NET creates an instance of MyDbContext on EVERY request, regardless of whether the MyDbContext was dependency injected into the controller.

My problem is, I do NOT want a database connection created for some of the endpoints in my application. How do I stop ASP.NET 8 from spinning up the Identity API (thus saving a database connection) for some endpoints?

I've tried reading the Microsoft Identity API docs and have not discovered any options yet that would let me accomplish what I'm trying to do.

I have verified that ASP.NET is creating a database connection for every request by logging the connections.

0

There are 0 best solutions below