# How to keep job state between runs?

**URL:** <https://discuss.hangfire.io/t/how-to-keep-job-state-between-runs/6846>\
**Category:** question\
**Tags:** recurring\
**Created:** [November 22, 2019, 12:59pm UTC](https://discuss.hangfire.io/t/how-to-keep-job-state-between-runs/6846 "2019-11-22T12:59:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![f0rza](https://avatars.discourse-cdn.com/v4/letter/f/b38774/32.png) [@f0rza](https://discuss.hangfire.io/u/f0rza)\
**Post date:** [November 22, 2019, 12:59pm UTC](https://discuss.hangfire.io/t/how-to-keep-job-state-between-runs/6846/1 "2019-11-22T12:59:20Z")

</div>

I have an instance of a class with a heavy constructor where all initialization happens. Then recurring job calls Execute method to make work done using instantiated stuff. Is there a way to keep this class instance between executions and not initialize from scratch?

---

<div class="post-metadata">

**Author:** ![hometoast](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/hometoast/32/688_2.png) [@hometoast](https://discuss.hangfire.io/u/hometoast)\
**Post date:** [November 22, 2019, 1:41pm UTC](https://discuss.hangfire.io/t/how-to-keep-job-state-between-runs/6846/2 "2019-11-22T13:41:40Z")

</div>

Built in to Hangire? No.

Are you using Dependency Injection container (Unity / AutoFac / StructureMap)?  
If so, you could do some trickery with the container and the JobActivationContext to get them to cache those objects.

Or just go the cheap and effective way. Don’t put them in the contructor, create those things statically (before you even start up the hangfire backgroundjobserver) and just use the static instance from inside your job method

---

<div class="post-metadata">

**Author:** ![f0rza](https://avatars.discourse-cdn.com/v4/letter/f/b38774/32.png) [@f0rza](https://discuss.hangfire.io/u/f0rza)\
**Post date:** [November 22, 2019, 1:48pm UTC](https://discuss.hangfire.io/t/how-to-keep-job-state-between-runs/6846/3 "2019-11-22T13:48:09Z")

</div>

using Ninject.

interesting, even if I do something like

```
            var d = new DistributionJob();
            RecurringJob.AddOrUpdate(() => d.Execute(), Cron.Minutely);

```

in Startup.Configuration (hangfire hosted in MVC project). Each minute constructor got executed. Very weird.

---

<div class="post-metadata">

**Author:** ![f0rza](https://avatars.discourse-cdn.com/v4/letter/f/b38774/32.png) [@f0rza](https://discuss.hangfire.io/u/f0rza)\
**Post date:** [November 22, 2019, 3:27pm UTC](https://discuss.hangfire.io/t/how-to-keep-job-state-between-runs/6846/4 "2019-11-22T15:27:38Z")

</div>

Found answer in docs:

> When a worker sees that the given method is an instance-method, it will activate its class first. By default, the `Activator.CreateInstance` method is used, so only classes with default constructors are supported by default. But you can plug in your IoC container and pass the dependencies through the constructor.
