Closed swidz closed 1 year ago
Started a discussion on Quartz.Net discussion group Help needed - System.NullReferenceException: Object reference not set to an instance of an object
For posterity,
I have found the solution. The issue was how assembly list was loaded that was passed to services.AddSilkierQuartz
To create that list, I used a helper method that internally used System.Reflection.Assembly.Load(assemblyName)
or System.Reflection.Assembly.LoadFile(assemblyPath)
but instead it should use
System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName)
or
System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath)
.
References: About System.Runtime.Loader.AssemblyLoadContext
Regards, Sebastian
I get the NullReferenceException if the job is not added using
.AddQuartzJob<ExecutionWorkerJobsMonitorJob>()
The error is as follows:
The AddSilkierQuartz method is called with assembly list. The job is properly decorated with SilkierQuartzAttribute
I expect the job to be discovered and triggered every 10 seconds I can see the job with trigger but every time they fire I get the
Object reference not set to an instance of an object.
error.I am using Microsoft SQLite as store .Net 7, with EFCore SilkierQuartz 5.0.356
If I add .AddQuartzJob() to register the job in services collection then the error does not happen.
please find parts of the code below:
My appsettings.json part related to Quartz
UPDATE: It is important to mention that
ExecutionWorkerJobsMonitorJob
is defined in an external assembly. When I define a job in the same assembly as the quartz host the job runs without problems. I was trying to do the same on example project but cannot reproduce - external assembly jobs run without problems Need to check if db storage has anything to do about it.UPDATE2: I changed the example project to use persistent storage on SQLServer I get the same System.NullReferenceException: Object reference not set to an instance of an object. It seems the issue is related to using job store with persistence, external assembly and autodiscover using SilkierQuartzAttribute
Working example: https://github.com/swidz/SilkierQuartz Job that returns
NullReferenceException
is calledHelloJobAutoExt
inTestJobs
assembly (project)