[英].NET Core 6 inject databasecontext to Singleton Service
I have a problem when try to use database context in one Singleton Service in my app.尝试在我的应用程序的一个 Singleton 服务中使用数据库上下文时遇到问题。
App.cs应用程序.cs
...
var service = builder.Services;
service.AddDbContext<MyDBContext>(options => options.UseSqlite(connectionString));
service.AddSingleton<MyMonitorManager>();
...
MyDBContext.cs MyDBContext.cs
public class MyDBContext : ApiAuthorizationDbContext<ApplicationUser>
{
public DroneDBContext(...): base(options, operationalStoreOptions){}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){}
protected override void OnModelCreating(ModelBuilder modelBuilder){}
}
MyMonitorManager.cs MyMonitorManager.cs
public class MyMonitorManager {
private readonly MyDBContext _databaseManager;
...
}
There are errors:有错误:
Cannot consume scoped service 'MyController.MyDBContext' from singleton 'MyController.Service.MyMonitorManager'.无法使用 singleton 'MyController.Service.MyMonitorManager' 中的范围服务 'MyController.MyDBContext'。
I try to search here and think about use like this:我尝试在这里搜索并考虑这样的使用:
MyDBContext dbContext = new MyDBContext();
service.AddSingleton(new MyMonitorManager(dbContext));
But not sure this is how I should do in .NET 6但不确定这是我在 .NET 6 中应该怎么做
You can use IServiceScopeFactory in your singleton class to create a new scope, and then get a db context reference from that:您可以在 singleton class 中使用 IServiceScopeFactory 来创建新的 scope,然后从中获取数据库上下文引用:
public class MyMonitorManager {
private readonly IServiceScopeFactory _scopeFactory;
public MyMonitorManager(IServiceScopeFactory _scopeFactory)
{
_scopeFactory = scopeFactory;
}
public SomeMethod()
{
using (var scope = _scopeFactory.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<MyDBContext>();
...
}
}
}
You can't inject directly scoped service into singleton, you have follwoing options:您不能将作用域服务直接注入 singleton,您有以下选项:
service.AddDbContext<MyDBContext>(options => options.UseSqlite(connectionString), ServiceLifetime.Transient);
IServiceScopeFactory
to create scope and resolve context when needed the singleton service:注入IServiceScopeFactory
以创建 scope 并在需要 singleton 服务时解析上下文:public class MyMonitorManager
{
private readonly IServiceScopeFactory _scopeFactory;
public class MyMonitorManager(IServiceScopeFactory scopeFactory) => _scopeFactory = scopeFactory;
public void SomeMethod()
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var ctx = scope.ServiceProvider.GetService<MyDBContext>();
// use context
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.