相关疑难解决方法(0)

升级到ASP.NET Core 2.0后,无法从singleton IActiveUsersService使用作用域服务IMongoDbContext

我今天更新了一个项目到ASP.NET Core 2,我收到以下错误:

不能从singleton IActiveUsersService使用作用域服务IMongoDbContext

我有以下注册:

services.AddSingleton<IActiveUsersService, ActiveUsersService>();
services.AddScoped<IMongoDbContext, MongoDbContext>();
services.AddSingleton(option =>
{
   var client = new MongoClient(MongoConnectionString.Settings);
   return client.GetDatabase(MongoConnectionString.Database);
})



public class MongoDbContext : IMongoDbContext
{
   private readonly IMongoDatabase _database;

   public MongoDbContext(IMongoDatabase database)
   {
      _database = database;
   }

   public IMongoCollection<T> GetCollection<T>() where T : Entity, new()
   {
      return _database.GetCollection<T>(new T().CollectionName);
   }
}

public class IActiveUsersService: ActiveUsersService
{

   public IActiveUsersService(IMongoDbContext mongoDbContext)
   {
      ...
   }
}
Run Code Online (Sandbox Code Playgroud)

为什么DI无法使用该服务?一切都适用于ASP.NET Core 1.1.

c# asp.net-core

47
推荐指数
4
解决办法
3万
查看次数

标签 统计

asp.net-core ×1

c# ×1