我们所有工作的Hangfire 1.5.3 System.MissingMethodException

时间:2016-01-29 16:00:17

标签: c# hangfire

我们刚刚将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,Boolean noCheck,Boolean&amp; canBeCached,   RuntimeMethodHandleInternal&安培; ctor,布尔&amp; bNeedSecurityCheck)at   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly,Boolean   skipCheckThis,Boolean fillCache,StackCrawlMark&amp; stackMark)at   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly,   Boolean skipCheckThis,Boolean fillCache,StackCrawlMark&amp; stackMark)
  在System.Activator.CreateInstance(Type type,Boolean nonPublic)at   System.Activator.CreateInstance(Type type)at   Hangfire.JobActivator.SimpleJobActivatorScope.Resolve(Type type)at   Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext   上下文)   Hangfire.Server.BackgroundJobPerformer&LT;&GT; c__DisplayClass3.b__0()   在   Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter   filter,PerformingContext preContext,Func 1 continuation) at Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable 1过滤器)at   Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context)   在Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context,   IStorageConnection连接,String jobId)

1 个答案:

答案 0 :(得分:3)

这个问题与新版本的Hangfire如何与Hangfire.Windsor软件包进行交互有关,这个软件只提供了一个自定义的JobActivator和一个用于服务定位的windsor容器。

移动

JobActivator.Current = new WindsorJobActivator(Container.Kernel);

在调用app.UseHangfireServer()之上,我们能够发现真正的异常消息,这是一个Castle.MicroKernel.ComponentNotFoundException,通知我们它无法连接包含我们想要挂起的方法的依赖项。跑。

我只想说,在1.3.4中,你可以做一份工作:

BackgroundJob.Enqueue(() => thingWhichDoesStuff.DoStuff()); 

其中 thingWhichDoesStuff 被注入到包含的类中,而hangfire.windsor JobActivator只能神奇地解析为该类型。

但是在1.5.3中,这个神奇的解决方案不再发生,所以你必须明确地告诉JobActivator包含你想要运行hangfire的方法的类型的接口:

BackgroundJob.Enqueue<IDoStuff>(x => x.DoStuff());  

这导致我们的所有工作再次开始工作。