让 Hangfire 与 ASP.NET MVC 和 LightInject 一起工作



我有一个 ASP.NET 的MVC应用程序,我最近升级为使用LightInject DI。 但是,即使使用 LightInject 扩展,我似乎也无法让 Hangfire 正常运行!

我在启动.cs中的 Hangfire 设置:

public void Configuration(IAppBuilder app)
{
var container = new ServiceContainer();
container.RegisterControllers(typeof(Web.Controllers.DashboardController).Assembly);
ConfigureServices(container);
ConfigureHangfire(container,app);
container.EnableMvc();
}
private void ConfigureHangfire(ServiceContainer container, IAppBuilder app)
{
var hangfireConnString = ConfigurationManager.ConnectionStrings["HfConnString"].ConnectionString;
GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseLightInjectActivator(container)
.UseSqlServerStorage(hangfireConnString, new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.FromSeconds(10),
UseRecommendedIsolationLevel = true,
UsePageLocksOnDequeue = true,
DisableGlobalLocks = true
});
var options = new DashboardOptions()
{
Authorization = new[] {new SystemAuthorizationFilter()}
};
app.UseHangfireDashboard("/hangfire",options);
app.UseHangfireServer();
}

但是,我在运行杭火作业时收到以下错误:

System.NullReferenceException: Object reference not set to an instance of an object.
at LightInject.Web.PerWebRequestScopeManager.GetOrAddScope() in C:projectslightinject-webbuildtmpNet46BinaryLightInject.WebLightInject.Web.cs:line 148
at LightInject.Web.PerWebRequestScopeManager.get_CurrentScope() in C:projectslightinject-webbuildtmpNet46BinaryLightInject.WebLightInject.Web.cs:line 129
at LightInject.ScopeManager.BeginScope() in C:projectslightinjectsrcLightInjectLightInject.cs:line 6091

我希望得到任何帮助! 提前非常感谢。

我实际上通过给hangfire自己的容器来解决这个问题。所以我的 ConfigureHangfire 方法的开始变成了:

private void ConfigureHangfire(ServiceContainer container, IAppBuilder app)
{
var hangfireConnString = ConfigurationManager.ConnectionStrings["HfConnString"].ConnectionString;
var container = new ServiceContainer();
ConfigureServices(container);
GlobalConfiguration.Configuration etc....

我不确定这是否完全正确,如果不是,我真的很想得到纠正!但无论如何,我希望这对某人有所帮助!

相关内容

  • 没有找到相关文章

最新更新