Cannot consume scoped service IMongoDbContext from singleton IActiveUsersService after upgrade to ASP.NET Core 2.0

前端 未结 4 1164
粉色の甜心
粉色の甜心 2020-11-30 05:42

I updated a project to ASP.NET Core 2 today and I get the following error:

Cannot consume scoped service IMongoDbContext from singleton IActiveUsersSe

4条回答
  •  无人及你
    2020-11-30 06:08

    You can also add

    .UseDefaultServiceProvider(options =>
                        options.ValidateScopes = false)
    

    before .Build() in Program.cs file to disable the validation.

    Try this only for development testing, ActiveUsersService is singleton and has a larger lifetime than MongoDbContext which is scoped and will not get disposed.

提交回复
热议问题