Question is similar to this which has no response yet.
My services are implemented similar to below(showing only the necessary pieces)
interface IBackgroundService{
Start()
}
class RecurringJob1 : IBackgroundService{
RecurringJob1(IServiceProvider sp){
//service provider will need to be injected to access configuration data
//load configuration data for this service
}
Start(){
//Perform actual work
}
}
All recurring services will adhere to above structure. All the available recurring job details are stored in appSettings.json similar to below
{
"Services":
[
{
Name: "CA.Services.RecurringJob1,
CronPattern: "xxxxxx",
IsActive: true/false
},
{
Name: "CA.Services.RecurringJob2,
CronPattern: "xxxxxx",
IsActive: true/false
}
]
}
Now I want to read above settings and register the recurring jobs. I know I can register jobs as below;
RecurringJob.AddOrUpdate<MyType>("name", e => e.Start(), cronpattern);
But problem is MyType = IBackgroundService and there are many. I can use reflection to create an instance from each type and register. But then I can’t use DI because I want my constructor of the service to get the serviceprovider at the execution time.
Is there a way to solve this?