我正在使用 asp.net mvc 3 和 quartz 调度程序。
目前我有这个
- 创建并执行作业。
- 转到我映射域的 Controller 以使用自动映射器查看模型
- 结果转到我要使用的 Controller Action Mailer
我得到一个错误
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 邮件程序
public EmailResult SendCalendarAppointmentNotifiation(CalendarAppointmentReminderVM vm)
{
To.Add(vm.To);
Subject = String.Format("Subject");
return Email("SendCalendarAppointmentEmail", vm);
}
在 return Email("SendCalendarAppointmentEmail", vm);
上死去(更容易使它们看起来更好,因为您可以使用母版页和强类型 View )。
Quartz 的作业执行不在 HttpRequest 中运行。 HttpContext.Current 也为 NULL。如果没有 asp.net 请求环境,这个 asp.net 邮件程序似乎无法工作(看看 StackTrace)
结帐Render a view as a string (呈现 View 、放入邮件并发送..),但不幸的是,在这篇文章中,所有示例也适用于当前的 ControllerContext/HttpContext。
我是一名优秀的程序员,十分优秀!