AI OnAI Off
You can use the ServiceLocator in those cases
var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
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/
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
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! :)
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?