在HttpContextWrapper中找不到JobRunShell.cs错误

时间:2017-07-11 17:22:01

标签: c# asp.net-mvc model-view-controller quartz.net httpcontext

我使用Quartz .NET每分钟执行代码并在代码执行后刷新Index页面,Quartz正常工作以执行代码但在重定向到Index页面(UI)时出错。

完整的代码是:

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);
    }
}

new HttpContextWrapper发生错误。 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

Quartz作业无法访问HttpContext。它们在HttpContext之外的单独线程中工作。