[newbie question] What context do jobs execute in?


#1

Hi,

First of all, thanks for working and making Hangfire available to the community, this is much appreciated. I have a pretty newbie questions, I’d appreciate if somebody could point me to the right direction.

My question is around what context do jobs get executed in. Let’s say I have this scenario (allow some pseudo-code):

class Controller {
    
   private IService _service;
   
   / Dependency Injection provides the service - let's say it's Transient
   Controller (IService service) {
      _service = service
   }
   
   ActionMethod() {
       // Incoming HTTP request
       // I now schedule a job
      BackgroundJob.Enqueue(
              () => **_service.doSomething(**)); 
   }
}

This job will get persisted in the database and at a certain point it will be executed. But in what context? What instance of the _service object will be used? Is that instance saved somewhere for Hangfire to be able to perform the job later? Also, if the app shuts down, and I restart it, obviously that instance of IService will be lost… how does Hangfire deal with that?

Hopefully my question makes sense or makes enough little sense that you can actually point me to some stuff to read to understand how the context is kept and recreated for jobs.

Thanks! :slight_smile: