Quantcast
Channel: Customized Commerce 13 and earlier versions
Viewing all articles
Browse latest Browse all 9642

Need help with calling a controller from a Scheduled Task

$
0
0

I need to be able to call a controller method from an EPiServer scheduled job and send email from the controller . The controller method work perfect if called normally and send the email, but not if called from the scheduled job.

In Startup I have this but it did not work: services.AddScoped<BaseScheduledTask, BaseScheduledTask>();

/// <summary>
/// Test Epi Job
/// </summary>
[EPiServer.PlugIn.ScheduledPlugIn(DisplayName = "Test EPi Job",
                                  Description = "Test EPi Job",
                                  GUID = "my GUID",
                                  Restartable = true,
                                  SortIndex = 1)]
public class TestEpiJob : BaseScheduledTask
{
    /// <summary>
    /// Test EPi Job
    /// </summary>
    /// <returns></returns>
    public override string Execute()
    {
        var controller = new HomeController(base.EmailMessaging, base.ExamplesMailer)
        {
            ControllerContext = new() 
        };
        string message = controller.EPiServerTestJob(true);
        return message;
    }
}
/// <summary>
/// Base class for all scheduled tasks
/// </summary>
public class BaseScheduledTask : EPiServer.Scheduler.ScheduledJobBase
{
    public IHttpContextAccessor HttpContextAccessor;
    public virtual EmailMessaging EmailMessaging { get; set; }
    public virtual ExamplesMailer ExamplesMailer { get; set; }
    public BaseScheduledTask(IHttpContextAccessor httpContextAccessor,
                             EmailMessaging emailMessaging,
                             ExamplesMailer webAdminMailer)
    {
        HttpContextAccessor = httpContextAccessor;
        EmailMessaging = emailMessaging;
        ExamplesMailer = webAdminMailer;
    }
    public BaseScheduledTask(EmailMessaging emailService) : base()
    {
        EmailMessaging = emailService;
    }
    public BaseScheduledTask(ExamplesMailer mailer) : base()
    {
        ExamplesMailer = mailer;
    }
    public override string Execute()
    {
        throw new System.NotImplementedException();
    }
}

Viewing all articles
Browse latest Browse all 9642

Trending Articles