I have a .NET 7 project that once a day creates a job that creates other jobs. The main job exists in a service, and the sub jobs each have their own services. Is it ok to inject the IBackgroundJobClient interface that has been set up as a Transient service? I notice that in the comments for the client it says to "Please create a new instance of a state class for each operation", I'm not sure if it applies to the "Create" method of the client.
builder.Services.AddTransient<IBackgroundJobClient, BackgroundJobClient>();
public MainJobService(IBackgroundJobClient client)
{
public void CreateJob() {
client.Create<ISubJobService>(myInterface => myInterface.CreateSubJobs());
}
}
public SubJobService(IBackgroundJobClient client)
{
public void CreateSubJobs() {
for (int x; x <10; x++)
{
client.Create<IJobService>(myInterface => myInterface.CreateJob());
}
}
}
It doesn't matter from perspective of the job. Every job is one scope - meaning that whenever you create a new job, that job will activated wia CoreBackgroundJobPerformer
that will use JobActivator
to create a new scope for that job.
So the main job of BackgroundJobClient
is creating the definition of the job that will later be activated wia Hangfire Server.
You can find more in depth in my other answer here