我正在使用一個調度程序,它將通過名爲actionMailer的庫發送提醒。然而,我需要httpcontext以及actionMailer使用mvc。這是爲什麼這個工作第一次罰款,但不是第二次?
所以我想出了這個:
// global aspx。
protected void Application_Start()
{
// Hook our DI stuff when application starts
IKernel kernel = SetupDependencyInjection();
// get the reminder service
IScheduledRemindersService scheduledRemindersService = kernel.Get<IScheduledRemindersService>();
RemindersController.iScheduledReminedrService = scheduledRemindersService;
RemindersController.StartScheduler();
}
//提醒控制器
private static HttpContext httpContext;
public static void StartScheduler()
{
// needed for action mailer
httpContext = System.Web.HttpContext.Current;
iScheduledReminedrService.StartAppointmentRemindersSchedule();
}
//計劃程序服務
public void StartAppointmentRemindersSchedule()
{
// get a scheduler and start it.
IScheduler scheduler = GetScheduler();
scheduler.Start();
// setup a job
JobDetail jobDetail = SetupJob("AppointmentRemindersJob", typeof(AppointmentRemindersJob));
//setup a trigger
SimpleTrigger trigger = SetupSimpleTrigger("AppointmentRemindersTrigger");
// schedule the job
scheduler.ScheduleJob(jobDetail, trigger);
}
//任用工作
public void Execute(JobExecutionContext context)
{
// code above to get CalendarAppointments
RemindersController.CalendarAppointmentsReminders(calendarAppointments);
}
//提醒控制器
[NonAction]
public static void CalendarAppointmentsReminders(List<CalendarAppointment> appointments)
{
// set it to have a http context so we can send out emails through mvc mailer.
System.Web.HttpContext.Current = httpContext;
List<CalendarAppointmentReminderVM> vm = Mapper.Map<List<CalendarAppointment>, List<CalendarAppointmentReminderVM>>(appointments);
foreach (var v in vm)
{
new EmailController().SendCalendarAppointmentNotifiation(v).DeliverAsync();
}
}
//電子郵件控制器
public EmailResult SendCalendarAppointmentNotifiation(CalendarAppointmentReminderVM vm)
{
To.Add(vm.To);
Subject = String.Format("Hi");
return Email("SendCalendarAppointmentEmail", vm);
}
完美作品我第一次啓動該應用程序並運行它。此後它崩潰的第二陣子
System.NullReferenceException是 未處理由用戶代碼不設置爲一個對象的一個實例
消息=對象引用。
源= WebDev.WebHost40堆棧跟蹤: 在Microsoft.VisualStudio.WebHost.Connection.get_RemoteIP() 在Microsoft.VisualStudio.WebHost.Connection.get_RemoteIP() 在Microsoft.VisualStudio.WebHost.Request.GetRemoteAddress() 在System.Web.HttpRequest.get_IsLocal() 在System.Web.HttpRequestWrapper.get_IsLocal() 在System.Web.WebPages.WebPageBase.ExecutePageHierarchy() 在System.Web.Mvc.WebViewPage.ExecutePageHierarchy() 的系統。 Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext,TextWriter writer, WebPageRenderingBase startPage) at System.Web.Mvc.R azorView.RenderView在System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext,TextWriter的作家,對象 實例) (ViewContext viewContext,TextWriter的作家) 在ActionMailer.Net.EmailResult.RenderViewAsString(ControllerContext 上下文,IVIEW視圖) 在ActionMailer.Net.EmailResult.AddMessageViews(ControllerContext 上下文) 在ActionMailer.Net.EmailResult.ExecuteResult(ControllerContext 上下文) 在ActionMailer.Net.MailerBase.Email(字符串 的viewName,對象模型,字符串 masterName) EmailController.SendCalendarAppointme上的 ntNotifiation(CalendarAppointmentReminderVM VM)在EmailController.cs:線80 在RemindersController.CalendarAppointmentsReminders(List`1 約會)在 RemindersController.cs:在AppointmentRemindersJob線61 。執行(JobExecutionContext 上下文)中 AppointmentRemindersJob.cs:線40 在Quartz.Core.JobRunShell.Run()
的InnerException:
其餘的錯誤在哪裏?具體來說,你會得到什麼錯誤? – NotMe 2011-05-20 18:40:40
@ Chris Lively - 更新。 – chobo2 2011-05-20 18:48:27
我只是想讓你知道我已經添加了一個獨立版本的ActionMailer,它不依賴於MVC。它的功能有限,但可以隨時查看:http://geeksharp.com/2011/07/06/actionmailer-0-6-released/ – 2011-09-26 15:32:08