Hangfire 1.5.3 System.MissingMethodException on all our jobs

本文关键字:on all our jobs MissingMethodException System Hangfire | 更新日期: 2023-09-27 18:32:36

我们刚刚将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 type, Boolean publicOnly, Boolean noCheck, Boolean&canBeCached, RuntimeMethodHandleInternal&ctor, Boolean&bNeedSecurityCheck( at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark&stackMark( at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark&stackMark(
at 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.<>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( at Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context, IStorage连接连接,字符串作业 ID(

Hangfire 1.5.3 System.MissingMethodException on all our jobs

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

通过移动

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

在对应用的调用上方。UseHangfireServer(( 我们能够发现真正的异常消息,这是一个 Castle.MicroKernel.ComponentNotFoundException,它通知我们它无法连接包含我们希望 hangfire 运行的方法的依赖项。

可以说,在 1.3.4 中,要运行一个作业,你可以这样做:

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

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

但是在 1.5.3 中,这种神奇的解决方案不再发生,因此您必须显式告诉 JobActivator 包含您希望 hangfire 运行的方法的类型的接口:

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

这导致我们所有的工作重新开始工作。