Hello,
I am writting an ASP.Net MVC Core 2.0 application.
I want to run a background task. I have read some documentation about IHostedService. I have written a Batch class which implements IHostedService and i have add this in Startup.cs:
services.AddSingleton<IHostedService, Batch>();
It works great.
Now, i want to access database from my background task. So i have injected my DbContext in Batch Constructor.
When i run the web application, i get this error:
Unhandled Exception: System.InvalidOperationException: Cannot consume scoped service 'myDbContext' from singleton 'Microsoft.Extensions.Hosting.IHostedService'.
I have read a lot of threads and documentations. I have understood the DI cannot inject scoped service in singleton but i have tried everything. Nothing works !
Thanks for your help