Try our conversational search powered by Generative AI!

Using dependency injection for my sheduled jobs?

Vote:
 

I stumbled upon some article mentioning it being possible to use dependency injection for scheduled jobs - great! But using the ScheduledPlugin attribute is dependent an Execute method which is static. How am I suppose to reach my instance member from the static method?

    [ScheduledPlugIn(GUID = "7CFBA546-6015-43C7-94AB-FB73DCF8A960")]
    public class CustomJob
    {
        private readonly IContentLoader _contentLoader;

        public CustomJob(IContentLoader contentLoader)
        {
            _contentLoader = contentLoader;
        }

        public static string Execute()
        {
            // How can I reach my _contentLoader?
        }
    }
#206802
Aug 30, 2019 9:00
Vote:
 

You can use the ServiceLocator in those cases

var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
#206812
Edited, Aug 30, 2019 10:40
Vote:
 

 Scheduled Job classes should inherit ScheduledJobBase which has an overridable method public override string Execute() which isn't static. Then you can use constructor injection as usual.

Here is the start of one of mine as example

    [ScheduledPlugIn(
        DisplayName = "Completed Order Export",
        Description = "Sends queued completed commerce orders to the back-office systems",
        GUID = JobGuidConstants.OrderExportJobId,
        SortIndex = JobOrderingConstants.OrderExportJobSortIndex
        )]
    public class OrderExportJob : ScheduledJobBase
    {
        private readonly INotificationManager _notificationManager;
        private readonly IOrderProcessor _orderProcessor;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="notificationManager">Notification manager</param>
        /// <param name="orderProcessor">The order processor</param>
        public OrderExportJob(INotificationManager notificationManager, IOrderProcessor orderProcessor)
        {
            _notificationManager = notificationManager;
            _orderProcessor = orderProcessor;
        }

        /// <summary>
        /// Execute the job
        /// </summary>
        /// <returns>System.String.</returns>
        public override string Execute()
        {
        }
    }

As documented here https://world.episerver.com/documentation/developer-guides/CMS/scheduled-jobs/

#206813
Edited, Aug 30, 2019 10:49
Vote:
 

It's also good practice to implement the Stop method too and make sure you're code can be stopped. I personally use Async methods and cancellation tokens for mine

#206814
Aug 30, 2019 10:52
Vote:
 

Di friendly jobs were introduced long time ago - https://blog.tech-fellow.net/2016/12/28/scheduled-jobs-updates/. Avoid static and you'll be good.

@Daniel - you owe me beer now because of keywords you mentioned! :)

#206837
Aug 31, 2019 14:39
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.