【发布时间】:2020-08-01 20:21:13
【问题描述】:
在 asp.net core 3.1 Web 应用程序中,我有一个从 BackgroundService 类继承的事件侦听器。 为什么注入了 DbContext 的 injectiong 仓库会出错?
启动:
services.AddDbContext<DBContext>(options => {
options.UseSqlServer("...connection string...");
});
services.AddTransient<ICalStatRepo, CalStatRepo>();
services.AddTransient<IHostedService, BackgroundListener>();
存储库:
public class CalStatRepo : ICalStatRepo
{
private readonly DBContext _context;
public CalStatRepo(DBContext context)
{
_context = context;
}
public async Task InsertCallStat(RawCallStatRegisterViewModel model)
{
var rawCall = new RawCallStat
{
HappenedAt = model.HappenedAt,
Source = model.Source,
Destination = model.Destination,
Status = model.Status
};
_context.Entry(rawCall).State = EntityState.Added;
try
{
await _context.SaveChangesAsync();
}
catch (Exception e)
{
throw new Exception("Insert new call stat fails with this error : " + e.Message);
}
}
}
后台服务:
public class BackgroundListener : BackgroundService
{
private readonly IServiceProvider _service;
public BackgroundListener(IServiceProvider service)
{
_service = service;
}
}
// what I want to do is insert logs into db here
private async void EvenetListener(Object sender, Event e)
{
var calStatRepo = _service.GetRequiredService<ICalStatRepo>(); //> Error
await calStatRepo.InsertCallStat(args);
}
问题是在事件监听器中添加所需的服务会报错如下:
无法从根提供程序解析“Repositories.ICalStatRepo”,因为 它需要范围服务“Models.Context.DBContext”。
DBContext在启动时添加为services.AddDbContext并注入CalStatRepo,然后在后台服务的事件监听器中添加为必需服务,但是为什么又需要作为作用域服务呢?
任何帮助将不胜感激。
【问题讨论】:
标签: c# .net-core entity-framework-core