Hi all, I have built most of my scheduling and automation app using Core 3 & Hangfire to handle the scheduling of specific tasks that can be run at specified intervals. Now this is working exactly as I need so long as each 'Job' service has a parameter-less constructor. Of course I am currently using the builtin DI that core provides and all is good with the world BUT I have what should be a pretty straight forward issue to resolve related to DI.
My 'Job' can be scheduled and if need to be also instantiated on request elsewhere in code (thinking of an email service for example) Now if I have a job that has a dependency that needs to be resolved at runtime I have 2 choices;
- I can keep my parameter-less constructor and simply instantiate whatever other dependencies I need in that same constructor. The problem with this is that it then makes DI pretty pointless if I have to manually create dependencies regardless of where in the app it gets used.
- The second option, and the one I am struggling with, is to configure a custom JobActivator for Hangfire that references the Container and can then resolved required dependencies. I have tried to do this but following an example but I get an error saying that ".Resolve' is not a valid method of Container.
This is the code that I am trying to use;
public class HangfireJobActivator : JobActivator
{
private IContainer _container;
public HangfireJobActivator(IContainer container)
{
_container = container;
}
public override object ActivateJob(Type type)
{
return _container.Resolve(type);
}
}
The bold, underscore text above is where my problem is. Is this something that existed in earlier version that has since been removed?
I would like to be able to get this working with the built in DI container BEFORE I start looking at other third party tools so can anybody guide me to where I am going wrong?
This is using Core 3.1 & Hangfire 1.7.10. Any help greatly appreciated as I am fairly new to Dot Net Core but I am enjoying it immensely so far!