如何为控制器创建自己的http上下文?

时间:2011-05-18 19:21:00

标签: c# asp.net-mvc asp.net-mvc-3 quartz-scheduler httpcontext

我正在使用asp.net mvc 3和quartz调度程序。

目前我有这个

  1. 创建并执行作业。
  2. 转到控制器,我将域映射到使用automapper查看模型
  3. 结果转到控制器,我想使用 Action Mailer
  4. 我收到错误

    System.ArgumentNullException was unhandled by user code
      Message=Value cannot be null.
    Parameter name: httpContext
      Source=System.Web
      ParamName=httpContext
      StackTrace:
           at System.Web.Routing.RequestContext..ctor(HttpContextBase httpContext, RouteData routeData)
           at ActionMailer.Net.MailerBase.Email(String viewName, Object model, String masterName)
           at EmailController.SendCalendarAppointmentNotifiation(CalendarAppointmentReminderVM vm) in EmailController.cs:line 73
           at RemindersController.CalendarAppointmentsReminders(List`1 taskReminders) in RemindersController.cs:line 54
           at QuartzJobs.AppointmentRemindersJob.Execute(JobExecutionContext context) in AppointmentRemindersJob.cs:line 39
           at Quartz.Core.JobRunShell.Run()
      InnerException: 
    

    //作业

    public void Execute(JobExecutionContext context)
            {
    
    
                                // some code to do some checking and to get results above(not shown)
                                RemindersController remindersController = new RemindersController();
                                remindersController.CalendarAppointmentsReminders(calendarAppointmentReminders);
    
                }
    

    // controller(做映射)\

       public void CalendarAppointmentsReminders(List<AppointmentReminder> appointments)
            {
               List<CalendarAppointmentReminderVM> vm = Mapper.Map<List<CalendarAppointment>, List<CalendarAppointmentReminderVM>>(appointments.Select(x => x.CalendarAppointment).ToList());
                Mapper.Map<List<AppointmentReminder>, List<CalendarAppointmentReminderVM>>(appointments, vm);
    
    
                foreach (var v in vm)
                {
                    new EmailController().SendCalendarAppointmentNotifiation(v);
                }
    
            }
    

    // mvc mailer

     public EmailResult SendCalendarAppointmentNotifiation(CalendarAppointmentReminderVM vm)
            {
                To.Add(vm.To);
                Subject = String.Format("Subject");
                return Email("SendCalendarAppointmentEmail", vm);
            }
    

    return Email("SendCalendarAppointmentEmail", vm);上死了我想使用它,因为我用它发送我所有的其他电子邮件,我发现它有更好的方式发送电子邮件(更容易使它们看起来更好,因为你可以使用主页面和强类型视图)。

1 个答案:

答案 0 :(得分:0)

Quartz的Job执行不在HttpRequest中运行。 HttpContext.Current也是NULL。 看来这个asp.net邮件程序在没有asp.net请求环境的情况下不起作用(hava一看StackTrace)

checkout Render a view as a string(渲染视图,放入邮件并发送..),但不幸的是,在这篇文章中,所有示例都适用于当前的ControllerContext / HttpContext。