hangfire Could not load file or assembly 'DynamicProxyGenAssembly2
Asked Answered
T

3

8

I have setup Hangfire as below:

 var t = IocManager.Resolve<TestJob>();

 RecurringJob.AddOrUpdate("sendDailyEmail",() => t.sendEmail(), Cron.Daily);

When I access the Hangfire dashboard, I can see the Recurring Jobs 1, but the job is not executed and I get this error:

Could not load file or assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

What is wrong with my setup?

Thorr answered 25/6, 2016 at 5:43 Comment(2)
Did you ever get this working?Sherrillsherrington
Did you solved?Coil
M
7

Might not be the same context as yours but I think it's still worth adding it here:
-Hangfire server was running as a Windows service;
-Hangfire dashboard running on top of a ASP.NET MVC 5 app;
-jobs were actually running but dashboard kept displaying a FileNotFoundException;

Solution: added a reference to the missing assembly (ie: 'DynamicProxyGenAssembly2') in the dashboard web app.

Same problem was reported and solved in the same way by @reggieboyYEAH.
Details here: https://github.com/HangfireIO/Hangfire/issues/558

Messaline answered 4/6, 2017 at 14:54 Comment(2)
How do you do that?Swansdown
@Swansdown - 2 ways. Either reference the project where DynamicProxyGenAssembly is or install it via NuGet. That is what I have to do. My executable code is located in one repo. As part of the build I pack it into a NuGet package and store it on our private nuget feed. I then install this package in the hangfire server (windows service) and web dashboard project respectively.Sneeze
S
3

You must ensure ALL apps that interact with Hangfire have the necessary dlls/assemblies to deserialize Jobs.

e.g. if your Hangfire dashboard is running from a different app from your Hangfire server then ensure they all have the needed dlls with the Job methods.

Another case I hit is where we had multiple servers with different versions of our software running using the same Hangfire database. Since the Job definition from one version used an assembly not available in our other version we got this System.IO.FileNotFoundException: Could not load file or assembly ... exception.

Even though we used queues to separate the jobs and ensure a server only ran jobs it should run ... if a job fails the first execution attempt then other Hangfire servers will try to look at the job and decide whether they should execute it. The 'queue' isn't an intrinsic part of a Job's definition so servers will still inspect jobs that are supposed to be processed by a different queue, and then if they don't have the right assemblies they can't deserialize the Job ... BOOM.

Handy references for this queue problem here and here and here.

Stunk answered 25/4, 2022 at 6:51 Comment(4)
I suffer from the same issue, however, non of my recurring jobs failed. I set them to run on a specific queue, but still, I get this exception. Can HangFure schedule a job from a different machine, even if it didn't fail?Lanceolate
@Lanceolate - yes, as I understand it, any server can pick up a job and inspect it and maybe try to run it. In my case I wasn't running RECURRING jobs, just one-off jobs. In the case of Recurring jobs I'd guess it's entirely random which server looks at the job first. Therefore you MUST ensure all apps have the dlls to deserialize all jobs.Stunk
So what's the point of queues then? :/Lanceolate
Yeah, I agree it's annoying. As I understand it, queues aren't really a fundamental top-level concept plus jobs can move between queues, which together means all servers need to be able to deserialize the job to inspect it and decide whether to process it. Lots of people aren't happy with this, but that's just how it's designed for now.Stunk
A
0

In my case, I'm using Autofac as the DI container and in the registration of the methods that I Enqueue/AddOrUpdate I included ".EnableInterfaceInterceptors()" to enable Interception when I switched briefly from Hangfire to Hosted Services, e.g.

builder.RegisterType<ExPartInvoiceBatchService>().As<IExPartInvoiceBatchService>().InstancePerDependency().EnableInterfaceInterceptors();

Anyway, when I switched back these processes started throwing these errors. If you use SQL for serialization, you can look in the Hangfire.Hash tables rows where Field = 'Job' and see that the entries for the problematic ones were serialized differently than the ones that work correctly. Why? I have no idea but I got rid of the Interception stuff and was back to everything working.

Aerify answered 16/4, 2021 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.