Recurring Hangfire jobs are failing with .NET 5 upgradation

Tags: #<Tag:0x00007f54fa7d0c48>

The hangfire recurring jobs which are scheduled using the following line are getting failed after we have upgraded our application into .NET 5.
RecurringJob.AddOrUpdate<>

The exact error I’m seeing is -
can not be scheduled due to job load exception.
Hangfire.Common.JobLoadException: Could not load the job. See inner exception for the details.
—> System.IO.FileNotFoundException: Could not load file or assembly ‘mscorlib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e’. The system cannot find the file specified.

The Hangfire versions we are using are -
Hangfire.AspNetCore “1.6.22”
Hangfire.Autofac =“2.3.1”
Hangfire.SqlServer = “1.6.22”

Try adding the following lines to your csproj file to ensure that mscorlib assembly isn’t trimmed out when publishing an application. The difference between development and staging/production environments can be explained by the fact that in development environment “dotnet publish” command isn’t called, and mscorlib package isn’t trimmed out in this case.

<ItemGroup>
    <TrimmerRootAssembly Include="mscorlib" />
</ItemGroup>

Please also note that Hangfire.Core and other assemblies licensed under LGPLv3 aren’t allowed to be statically linked (e.g. merged into a single executable) with your application, it is only allowed to reference them as separate DLL files. Static linking is only possible with Business or Enterprise subscriptions that comes with a commercial EULA for Hangfire.Core packages and thus allow static linking.