-1

I using Quartz .NET to execute code every minute and also refresh Index page after code execution, Quartz working fine to execute code but error while redirect to Index page (UI).

Complete code is :

Startup.cs

public void Configuration(IAppBuilder app)
{
    ConfigureAuth(app);

    try
    {
        // construct a scheduler factory
        ISchedulerFactory schedFact = new StdSchedulerFactory();

        // get a scheduler
        IScheduler sched = schedFact.GetScheduler();
        sched.Start();

        // define the job and tie it to our HelloJob class
        IJobDetail job = JobBuilder.Create<NotificationJob>()
            .WithIdentity("myJob", "group1")
            .Build();

        // Trigger the job to run now, and then every 60 seconds
        ITrigger trigger = TriggerBuilder.Create()
          .WithIdentity("myTrigger", "group1")
          .StartNow()
          .WithSimpleSchedule(x => x
              .WithIntervalInSeconds(60)
              .RepeatForever())
          .Build();

        sched.ScheduleJob(job, trigger);
    }
    catch (ArgumentException e) { }
}

NotificationJob.cs

public class NotificationJob : IJob
{
    public void Execute(IJobExecutionContext Context)
    {
       // Code execution logic here...

        // Redirect to Index page
        var context = new RequestContext(new HttpContextWrapper(System.Web.HttpContext.Current), new RouteData());
        var urlHelper = new UrlHelper(context);
        var url = urlHelper.Action("Index", "Home");
        System.Web.HttpContext.Current.Response.Redirect(url);
    }
}

Error occurs at new HttpContextWrapper. Any help is appreciated.

Shri
  • 351
  • 3
  • 16

1 Answers1

0

Quartz jobs have no way of accessing HttpContext. They work outside of HttpContext, in separate threads.

Marko Lahma
  • 6,586
  • 25
  • 29