HangfireIO / HangfireIO/Hangfire
Multiple injection of the same interface problem
- Dominant language
- C#
- Stars
- 10.1k
- Forks
- 1.8k
- Avg merge
- 1h 19m
- Merged PRs (30d)
- 1
Description
I have multiple injection of the same interface IBootOptions in my project to get the different options for the recurring jobs where we can set an Action called LongRunningFunction
```
services.AddTransient(s => new BootOptions
{
LongRunningFunction = () => s.GetService().RetrieveTitleList()
});
services.AddTransient(s => new BootOptions
{
LongRunningFunction = () => s.GetService().RetrieveSubTitleList()
});
```
I got a class Bootable that implement a method that will execute the LongRunningFunction when the recurring job are triggered
```
public class Bootable : IBootable
{
public Bootable(IBootOptions bootOptions)
{
BootOptions = bootOptions;
}
public void Execute()
{
BootOptions.LongRunningFunction();
}
}
```
and finally in the startups, I declare all the recurring job
```
public void Configure(IServiceProvider serviceProvider)
{
foreach (var bootOptions in serviceProvider.GetServices())
recurringJobManager.AddOrUpdate(bootOptions.Job.Identity,
() => new Bootable(serviceProvider.GetService(),
serviceProvider.GetService(), bootOptions).Execute(), bootOptions.Job.CronSchedule);
}
```
My problem is that when the recurring job are triggered, the bootOptions is always the last injected bootOptions. So in my example the LongRunningFunction RetrieveSubTitleList will be executed two times and the RetrieveTitleList will never be executed. Hangfire recreate the Bootable object when it is triggered and the injected bootOptions is always the last. Is there a way to avoid hangfire to recreate the Bootable object when the recurring jobs are triggered ?
Contributor guide
Research direction
Start by reproducing the behavior from the AddTransient registrations and the Configure method that enumerates GetServices. Trace how the recurring job invokes Bootable.Execute and determine whether each job retains its intended options. Done means the two recurring jobs execute their respective LongRunningFunction values without relying on undocumented behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100