Answered by:
Core 3, Hangfire & DI

Question
-
User-331009151 posted
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!
Tuesday, April 7, 2020 3:14 PM
Answers
-
User-474980206 posted
the container either implements Resolve, or you call the the containers object constructor. the method name will depend on the container you use. see
http://docs.hangfire.io/en/latest/background-methods/using-ioc-containers.html
if you used .net core services. it would be:
public class HangfireJobActivator : JobActivator { private IServiceProvider _container; public HangfireJobActivator(IServiceProvider container) { _container = container; } public override object ActivateJob(Type type) { return _container.GetRequiredService(type); } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 7, 2020 3:31 PM
All replies
-
User475983607 posted
Rather than trying to execute the job, can't you add an adhoc schedule?
Tuesday, April 7, 2020 3:29 PM -
User-474980206 posted
the container either implements Resolve, or you call the the containers object constructor. the method name will depend on the container you use. see
http://docs.hangfire.io/en/latest/background-methods/using-ioc-containers.html
if you used .net core services. it would be:
public class HangfireJobActivator : JobActivator { private IServiceProvider _container; public HangfireJobActivator(IServiceProvider container) { _container = container; } public override object ActivateJob(Type type) { return _container.GetRequiredService(type); } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 7, 2020 3:31 PM -
User-331009151 posted
Thanks for your reply mgebhard, yes that is what I did do first but was told to find another way.
Wednesday, April 8, 2020 9:33 AM -
User-331009151 posted
Thank you for your reply Bruce I believe this is the answer that I needed, thanks.
Wednesday, April 8, 2020 9:34 AM