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

You can’t use a service with a smaller lifetime. Scoped services only exist per-request, while singleton services are created once and the instance is shared.

Now only one instance of IActiveUsersService exists in the app. But it wants to depend on MongoDbContext, which is Scoped, and is created per-request.

You will have to either:

  1. Make MongoDbContext a Singleton, or
  2. Make IActiveUsersService Scoped, or
  3. Pass MongoDbContext into the user service as a function argument

Leave a Comment