Cannot consume scoped service 'MyDbContext' from singleton 'Microsoft.AspNetCore.Hosting.Internal.HostedServiceExecutor'

前端 未结 4 487
遥遥无期
遥遥无期 2021-01-07 18:21

I\'ve build a background task in my ASP.NET Core 2.1 following this tutorial: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-

4条回答
  •  粉色の甜心
    2021-01-07 18:57

    You still need to register MyDbContext with the service provider. Usually this is done like so:

    services.AddDbContext(options => {
        // Your options here, usually:
        options.UseSqlServer("YourConnectionStringHere");
    });
    

    If you also posted your Program.cs and Startup.cs files, it may shed some more light on things, as I was able to quickly setup a test project implementing the code and was unable to reproduce your issue.

提交回复
热议问题