'hangfire Could not load file or assembly 'DynamicProxyGenAssembly2

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?



Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rozerocool
Solution 2 Will
Solution 3 Rory