我们所有工作上的Hangfire 1.5.3 System.MissingMethodException [英] Hangfire 1.5.3 System.MissingMethodException on all our jobs

查看:117
本文介绍了我们所有工作上的Hangfire 1.5.3 System.MissingMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们刚刚将hangfire从1.3.4更新为1.5.3.

We just updated hangfire from 1.3.4 to 1.5.3.

我们的创业公司已经改变了:

Our startup has changed from this:

    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种不同的工作)都会因以下错误而立即失败:

Now all our jobs (we have 4 different kinds of jobs) fail immediately with this error:

System.MissingMethodException:未定义无参数构造函数 对于这个对象.在 System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,布尔 publicOnly,布尔值noCheck,布尔值& canBeCached, RuntimeMethodHandleInternal& ctor,布尔值和放大器; bNeedSecurityCheck) System.RuntimeType.CreateInstanceSlow(布尔publicOnly,布尔 skipCheckThis,布尔值fillCache,StackCrawlMark& stackMark) System.RuntimeType.CreateInstanceDefaultCtor(布尔publicOnly, 布尔型skipCheckThis,布尔型fillCache,StackCrawlMark& stackMark)
在System.Activator.CreateInstance(Type type,Boolean nonPublic)在 System.Activator.CreateInstance(Type type)位于 Hangfire.JobActivator.SimpleJobActivatorScope.Resolve(Type type)在 Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext 上下文) Hangfire.Server.BackgroundJobPerformer.<> c__DisplayClass3.b__0() 在 Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter 过滤器,PerformingContext preContext,Func 1 continuation) at Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable 1过滤器)位于 Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext上下文) 在Hangfire.Server.Worker.PerformJob(BackgroundProcessContext上下文中, IStorageConnection连接,字符串jobId)

System.MissingMethodException: No parameterless constructor defined for this object. at 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 context) at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass3.b__0() at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func1 continuation) at Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable1 filters) at Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context) at Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context, IStorageConnection connection, String jobId)

推荐答案

问题出在新版Hangfire如何与

Well the problem had to do with some magic in how the new version of Hangfire interacted with the Hangfire.Windsor package, which simply provides a custom JobActivator with a windsor container for service location.

通过移动

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

在对app.UseHangfireServer()的调用之上,我们能够发现真正的异常消息,这是一个Castle.MicroKernel.ComponentNotFoundException,通知我们它无法加入包含我们想要执行hangfire的方法的依赖项.

above the call to app.UseHangfireServer() we were able to uncover the real exception message, which was a Castle.MicroKernel.ComponentNotFoundException informing us that it couldn't wire in the dependency containing the method we wanted hangfire to run.

在1.3.4中可以说,要执行一项工作,您可以执行以下操作:

Suffice to say, in 1.3.4, to run a job you can do this:

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

其中将 thingWhichDoesStuff 注入到包含的类中,并且hangfire.windsor JobActivator能够神奇地解析为该类型.

where thingWhichDoesStuff is injected into the containing class and the hangfire.windsor JobActivator is just magically able to resolve to the type.

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

However in 1.5.3 this magic resolution no longer happens, so you must explicitly tell the JobActivator the interface of the type containing the method you want hangfire to run:

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

这会使我们所有的工作重新开始.

This causes all our jobs to start working again.

这篇关于我们所有工作上的Hangfire 1.5.3 System.MissingMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆