Search code examples
c#.netasp.net-core-webapihangfire.net-8.0

Could not load type 'ScheduledJobs.ExchangeRateProcess.ExchangeRateJob' from assembly 'Job.ScheduledJobs, Version=4.0.0.0...'


I am developing a feature that fetches exchange rates from the central bank and periodically writes them to the database. I am using Hangfire to schedule the job to run at specific intervals.

When I run the project, I can see my job in the Recurring Jobs tab, and the job runs once, successfully writing data to the database. However, afterwards, it throws the following error. While it produces this error, if I manually trigger the job, it works, but then it does not perform the periodic writing process as expected.

I upgraded the project to .NET 8.0, and all my packages are up to date. I initially thought the error might be due to the upgrade, so I also tested it with .NET 5.0, but I encountered the same error.

By the way, the error persists whether the version is 3.0.0.2 or 4.0.0.1. Regardless of the version, it seems to be looking for version 4.0.0.0 and getting stuck in this version.

System.InvalidOperationException: Recurring job can't be scheduled, see inner exception for details.
 ---> Hangfire.Common.JobLoadException: Could not load the job. See inner exception for the details.
 ---> System.TypeLoadException: Could not load type 'Manager.Job.ScheduledJobs.ExchangeRateProcess.ExchangeRateJob' from assembly 'Manager.Job.ScheduledJobs, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null'.
   at System.Reflection.RuntimeAssembly.GetTypeCore(String typeName, ReadOnlySpan`1 nestedTypeNames, Boolean throwOnError, Boolean ignoreCase)
   at System.Reflection.TypeNameParser.GetType(String typeName, ReadOnlySpan`1 nestedTypeNames, String assemblyNameIfAny)
   at System.Reflection.TypeNameParser.NamespaceTypeName.ResolveType(TypeNameParser& parser, String containingAssemblyIfAny)
   at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
   at Hangfire.Common.TypeHelper.TypeResolver(Assembly assembly, String typeName, Boolean ignoreCase)
   at System.Reflection.TypeNameParser.GetType(String typeName, ReadOnlySpan`1 nestedTypeNames, String assemblyNameIfAny)
   at System.Reflection.TypeNameParser.NamespaceTypeName.ResolveType(TypeNameParser& parser, String containingAssemblyIfAny)
   at System.Type.GetType(String typeName, Func`2 assemblyResolver, Func`4 typeResolver, Boolean throwOnError)
   at Hangfire.Common.TypeHelper.DefaultTypeResolver(String typeName)
   at Hangfire.Storage.InvocationData.DeserializeJob()
   --- End of inner exception stack trace ---
   at Hangfire.Storage.InvocationData.DeserializeJob()
   at Hangfire.RecurringJobEntity..ctor(String recurringJobId, IDictionary`2 recurringJob, ITimeZoneResolver timeZoneResolver, DateTime now)
   --- End of inner exception stack trace ---
   at Hangfire.Server.RecurringJobScheduler.ScheduleRecurringJob(BackgroundProcessContext context, IStorageConnection connection, String recurringJobId, RecurringJobEntity recurringJob, DateTime now)

It's a weird situation. Has anyone get and resolved this error before?

I have checked the GAC, but the dlls there are usually System dlls or NuGet package dlls. They were not found in C:\Windows\Microsoft.NET\assembly.

When I decompiled the dll specific to the app using dotPeek, everything seems fine. My versions are 4.0.0.1, everything is up to date, and the DLL in the bin folder of the relevant Jobs.ScheduledJobs class library includes Jobs.ScheduledJobs.ExchangeRateProcess.ExchangeRateJob.

dotPeek screenshot

Enabling the "Copy to local" option for the relevant reference in the project did not resolve the issue. Similarly, performing a Clean-Rebuild did not solve the problem neither.


Solution

  • The root cause of the issue was multiple jobs concurrently operating on the same database. There was already too much data in the connected database. To resolve this, I created a new database and redirected the jobs to it, which fixed the problem.