# Dynamic recurring job registration

**URL:** <https://discuss.hangfire.io/t/dynamic-recurring-job-registration/8983>\
**Category:** question\
**Tags:** recurring, aspnetcore\
**Created:** [February 19, 2022, 5:30am UTC](https://discuss.hangfire.io/t/dynamic-recurring-job-registration/8983 "2022-02-19T05:30:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![modithak\_K](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/modithak_k/32/2832_2.png) [@modithak\_K](https://discuss.hangfire.io/u/modithak_K)\
**Post date:** [February 19, 2022, 5:30am UTC](https://discuss.hangfire.io/t/dynamic-recurring-job-registration/8983/1 "2022-02-19T05:30:45Z")

</div>

Question is similar to [this](http://hangfire.discourse.group/t/scheduling-a-recurring-job-with-a-dynamic-type-from-a-config-solution/4346) 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?

---

<div class="post-metadata">

**Author:** ![aschenta](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/aschenta/32/2837_2.png) [@aschenta](https://discuss.hangfire.io/u/aschenta)\
**Post date:** [February 25, 2022, 3:53pm UTC](https://discuss.hangfire.io/t/dynamic-recurring-job-registration/8983/2 "2022-02-25T15:53:16Z")

</div>

I would formalize appsettings as a dictionary or a config class and add that to your DI. Then specify that in the constructor.
