# Creating a BatchJob scoped service using Hangfire.Autofac

**URL:** <https://discuss.hangfire.io/t/creating-a-batchjob-scoped-service-using-hangfire-autofac/11310>\
**Category:** question\
**Tags:** hangfire-pro, aspnetcore\
**Created:** [May 27, 2025, 11:59am UTC](https://discuss.hangfire.io/t/creating-a-batchjob-scoped-service-using-hangfire-autofac/11310 "2025-05-27T11:59:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cynicszm](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/cynicszm/32/2484_2.png) [@Cynicszm](https://discuss.hangfire.io/u/Cynicszm)\
**Post date:** [May 27, 2025, 11:59am UTC](https://discuss.hangfire.io/t/creating-a-batchjob-scoped-service-using-hangfire-autofac/11310/1 "2025-05-27T11:59:19Z")

</div>

Hi,

We create Batch jobs with nested Batches and Jobs. The JobActivator uses the Hangfire.Autofac package and UseAutofacActivator when updating IGlobalConfiguration

We would like to have a type registered at the BatchJob level. There is InstancePerBackgroundJob() as shown in the snippet below, is there an equivalent for a **BatchJob**?

```auto
 configuration
       .UseAutofacActivator( builder.Build(), ( builder, context ) =>
       {
           builder.RegisterType<SomeType>().InstancePerBackgroundJob();
       }

```

How do we register a type (SomeType) to be available for the life time of a **BatchJob and all its nested children**?

Thanks,  
John

---

<div class="post-metadata">

**Author:** ![odinserj](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/odinserj/32/648_2.png) [@odinserj](https://discuss.hangfire.io/u/odinserj)\
**Post date:** [May 28, 2025, 9:33am UTC](https://discuss.hangfire.io/t/creating-a-batchjob-scoped-service-using-hangfire-autofac/11310/2 "2025-05-28T09:33:51Z")

</div>

Batches consist of regular background jobs, so `InstancePerBackgroundJob` will also be useful for background jobs that are part of a batch. Have you tried using this?

---

<div class="post-metadata">

**Author:** ![Cynicszm](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/cynicszm/32/2484_2.png) [@Cynicszm](https://discuss.hangfire.io/u/Cynicszm)\
**Post date:** [May 28, 2025, 9:55am UTC](https://discuss.hangfire.io/t/creating-a-batchjob-scoped-service-using-hangfire-autofac/11310/3 "2025-05-28T09:55:58Z")

</div>

The way we organise batches means that multiple background jobs are used.  
e.g.  
An overall batch that consists of three child batches.

- An Init batch that configures multiple steps consisting of batches and background jobs
- A Process batch that executes multiple steps consisting of batches and background jobs
- A Complete batch that finalises multiple steps consisting of batches and background jobs

I would like to resolve the service for the lifetime from Init until Complete

---

<div class="post-metadata">

**Author:** ![odinserj](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/odinserj/32/648_2.png) [@odinserj](https://discuss.hangfire.io/u/odinserj)\
**Post date:** [May 29, 2025, 7:52am UTC](https://discuss.hangfire.io/t/creating-a-batchjob-scoped-service-using-hangfire-autofac/11310/4 "2025-05-29T07:52:28Z")

</div>

I’m afraid this is not possible, since all the 3 background jobs in a batch don’t share process’ state and can be executed even on different states. So it’s better to store intermediary data in an external storage, by using batch id form example to address it.

---

<div class="post-metadata">

**Author:** ![Cynicszm](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/cynicszm/32/2484_2.png) [@Cynicszm](https://discuss.hangfire.io/u/Cynicszm)\
**Post date:** [May 29, 2025, 8:10am UTC](https://discuss.hangfire.io/t/creating-a-batchjob-scoped-service-using-hangfire-autofac/11310/5 "2025-05-29T08:10:19Z")

</div>

Thank you for the update, I’ll have a look at using the batch id
