Base solution for your next web application
Open Closed

Inject service to HangFire Job #1455


User avatar
0
phoenix created

Good day,

I would like to inject a service to HangFire job. How to properly do it ? I have following code: Global.asax:

_container = new WindsorContainer();
_container.Register(Castle.MicroKernel.Registration.Component.For<IListingImageAppService().ImplementedBy(typeof(ListingImageAppService)).LifestyleSingleton());
JobActivator.Current = new WindsorJobActivator(_container.Kernel);

My JobBundle config:

WindsorContainer con = new WindsorContainer();
ImagesImportJob job = new ImagesImportJob(con.Resolve<ListingImageAppService>());
RecurringJob.AddOrUpdate("JobId", () => job.ImportImages(), Cron.Daily);

And my HangFire Job:

public class ImagesImportJob
{
   private readonly ListingImageAppService _srv;
   public ImagesImportJob(ListingImageAppService srv)  { _srv = srv; }
   public void ImportImages()
   {
        ///
   }
}
Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Why don't you derive your ImagesImportJob from ITransientDependecy and inject it to class you use it. In that way you can incject onter services into ImagesImportJob.

    You can see a similar example here <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Background-Jobs-And-Workers">http://aspnetboilerplate.com/Pages/Docu ... nd-Workers</a>

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)