I have simple DbContext with interface which is registered as scoped service:
services.AddDbContext<ISomeDbContext, SomeDbContext>(cfg =>
{
cfg.UseSqlServer(configuration.GetConnectionString("Default"), cfg =>
{
cfg.MigrationsAssembly(typeof(SomeDbContext).Assembly.FullName);
});
});
Everything works fine, but up to a point. Some operations do not finish when I hold down F5 on the page, so I start spamming with refresh. Then I get an exception:
An unhandled exception has occurred while executing the request.
System.Threading.Tasks.TaskCanceledException: A task was canceled.
I can notice dozens of such exceptions, depending on how many times I refresh the page.
Operations finish when using DbContext as a transient (but it seems inefficient to me as I create a new instance on every request). However, a lot of exceptions are still thrown.
Besides, I can add that I only work on DbContext asynchronously (except for one Attach method call) and use the CancellationToken
taken from HttpContext.RequestAborted
everywhere.