When running my microservice developed with ASP.NET Core and EF Core (latest versions) I started getting this error:
Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
This is very strange, as it worked fine until tomorrow. SQL database is hosted in Docker.
This exception is thrown by code:
try
{
using var c = ctx.Database.GetDbConnection();
var cconstrcc = c.ConnectionString;
var dbstate = c.State;
c.Open(); // <- exception thrown
ctx.Database.OpenConnection();
}
catch (Exception ex)
{
}
This is only test code, because application just hangs when I try to run query dbContext.SomeTable.ToList() (does not work with async version as well), and I am unable to see why and when it happens (I suspected some deadlock maybe).
This is very very strange, as it happens on the first attempt to connect to database.
I was inspecting SQL Server Monitor, SQL Server Profiler and also run stored procedure sp_who, but I don't see ANYTHING, any connection made from my app, it's like SQL pool exhausts immediately.
Also, we used dependency injection everywhere, but I have gone through all context usages and switched it to local variable with
using var ctx = new DbContext();
to make sure I close every connection. Issue is still exactly the same.
UPDATE
But I also run some test application (console app) and made connection simply by using SqlConnection object, without any ORM - it worked.
UPDATE
Also tried to go back in history with git checkout to point where it was definietely working - to my surprise, that version also hangs and does not work.
UPDATE
Changing from IIS to Kestrel resolved the issue, connections are made successfully. So it's IIS related bug ?