DI 的 asp.net 核心 webapi 问题中的 Hangfire
Hangfire in asp.net core webapi problem with DI
当我执行后台作业并尝试解析所有依赖项时,出现异常:
Microsoft.EntityFrameworkCore.Query[10100]
An exception occurred while iterating over the results of a query for context type 'MyProj.DAL.ApplicationContext'.
System.ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'ApplicationContext'
at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
我的代码 - 在 ConfigureServices(IServiceCollection services)
:
services.AddHangfire(cfg => cfg
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseMemoryStorage());
services.AddHangfireServer();
在Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider provider)
中:
GlobalConfiguration.Configuration.UseActivator(new ContainerJobActivator(provider));
app.UseHangfireDashboard()
和
public class ContainerJobActivator : JobActivator
{
private IServiceProvider _container;
public ContainerJobActivator(IServiceProvider container)
{
_container = container;
}
public override object ActivateJob(Type type)
{
return _container.GetService(type);
}
}
我的数据库上下文
services.AddEntityFrameworkNpgsql().
AddDbContext<ApplicationContext>(opt =>
opt.UseNpgsql("server=localhost;port=5432;database=database;uid=root;password=password;"));
我们需要在工作中解析 IServiceScopeFactory,而不是获取我们的服务
using (var scope = _factoryScope.CreateScope())
{
_articleRepository = scope.ServiceProvider.GetService<IArticleRepository>() ?? throw new ArgumentNullException();
// your job here.....
}
当我执行后台作业并尝试解析所有依赖项时,出现异常:
Microsoft.EntityFrameworkCore.Query[10100]
An exception occurred while iterating over the results of a query for context type 'MyProj.DAL.ApplicationContext'.
System.ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'ApplicationContext'
at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
我的代码 - 在 ConfigureServices(IServiceCollection services)
:
services.AddHangfire(cfg => cfg
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseMemoryStorage());
services.AddHangfireServer();
在Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider provider)
中:
GlobalConfiguration.Configuration.UseActivator(new ContainerJobActivator(provider));
app.UseHangfireDashboard()
和
public class ContainerJobActivator : JobActivator
{
private IServiceProvider _container;
public ContainerJobActivator(IServiceProvider container)
{
_container = container;
}
public override object ActivateJob(Type type)
{
return _container.GetService(type);
}
}
我的数据库上下文
services.AddEntityFrameworkNpgsql().
AddDbContext<ApplicationContext>(opt =>
opt.UseNpgsql("server=localhost;port=5432;database=database;uid=root;password=password;"));
我们需要在工作中解析 IServiceScopeFactory,而不是获取我们的服务
using (var scope = _factoryScope.CreateScope())
{
_articleRepository = scope.ServiceProvider.GetService<IArticleRepository>() ?? throw new ArgumentNullException();
// your job here.....
}