我们刚刚将hangfire从1.3.4更新到1.5.3。
我们的创业公司已经改变了:
private static void DoHangfire(IAppBuilder app)
{
var options = new BackgroundJobServerOptions
{
// Set thread count to 1
WorkerCount = 1
};
app.UseHangfire(config =>
{
config.UseSqlServerStorage(ConfigurationManager.ConnectionStrings["JobsDB"].ConnectionString);
config.UseAuthorizationFilters(new HangfireDashboardAuthorizationFilter());
config.UseServer(options);
});
// Set retries to zero
GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 0 });
JobActivator.Current = new WindsorJobActivator(Container.Kernel);
}
对此:
private static void DoHangfire(IAppBuilder app)
{
var options = new BackgroundJobServerOptions
{
// Set thread count to 1
WorkerCount = 1
};
GlobalConfiguration.Configuration.UseSqlServerStorage(
ConfigurationManager.ConnectionStrings["JobsDB"].ConnectionString);
app.UseHangfireDashboard("/hangfire", new DashboardOptions()
{
AuthorizationFilters = new List<IAuthorizationFilter>
{
new HangfireDashboardAuthorizationFilter()
}
});
app.UseHangfireServer(options);
// Set retries to zero
GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 0 });
JobActivator.Current = new WindsorJobActivator(Container.Kernel);
}
现在我们所有的工作(我们有4种不同的工作)都会立即失败并出现此错误:
System.MissingMethodException:没有为此对象定义的无参数构造函数。在System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,布尔publicOnly,布尔NOCHECK,布尔逻辑canBeCached,RuntimeMethodHandleInternal&构造函数,布尔逻辑bNeedSecurityCheck)在System.RuntimeType.CreateInstanceSlow(布尔publicOnly,布尔skipCheckThis,布尔fillCache,StackCrawlMark&stackMark)在System.RuntimeType.CreateInstanceDefaultCtor (Boolean publicOnly,Boolean skipCheckThis,Boolean fillCache,StackCrawlMark&stackMark) 在位于Hangfire的Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context)的Hangfire.JobActivator.SimpleJobActivatorScope.Resolve(Type type)的System.Activator.CreateInstance(Type type,Boolean nonPublic)中的System.Activator.CreateInstance(Type type,Boolean nonPublic)。位于Hangfire.Server.Worker.PerformJob的Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context)中的Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter过滤器,PerformingContext preContext,Func
1 continuation) at Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable
1过滤器)中的Server.BackgroundJobPerformer。<> c__DisplayClass3.b__0() BackgroundProcessContext上下文,IStorageConnection连接,String jobId)
好吧,这个问题与新版本的Hangfire如何与Hangfire.Windsor软件包进行交互有一些魔力,JobActivator.Current = new WindsorJobActivator(Container.Kernel);
软件包只是提供一个自定义JobActivator和一个用于服务定位的windsor容器。
通过移动
BackgroundJob.Enqueue(() => thingWhichDoesStuff.DoStuff());
在对app.UseHangfireServer()的调用之上,我们能够发现真正的异常消息,这是一个Castle.MicroKernel.ComponentNotFoundException,通知我们它无法连接包含我们想要运行hangfire的方法的依赖项。
可以说,在1.3.4中,要完成一项工作,你可以这样做:
BackgroundJob.Enqueue<IDoStuff>(x => x.DoStuff());
whereWhichDoesStuff被注入到包含类中,而hangfire.windsor JobActivator只能神奇地解析为该类型。
但是在1.5.3中,这个神奇的解决方案不再发生,所以你必须明确地告诉JobActivator包含你想要运行hangfire的方法的类型的接口:
qazxswpoi
这导致我们所有的工作再次开始工作。