2011-05-20 143 views
0

我正在使用一個調度程序,它將通過名爲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:

+0

其餘的錯誤在哪裏?具體來說,你會得到什麼錯誤? – NotMe 2011-05-20 18:40:40

+0

@ Chris Lively - 更新。 – chobo2 2011-05-20 18:48:27

+1

我只是想讓你知道我已經添加了一個獨立版本的ActionMailer,它不依賴於MVC。它的功能有限,但可以隨時查看:http://geeksharp.com/2011/07/06/actionmailer-0-6-released/ – 2011-09-26 15:32:08

回答

0

的HttpContext不會是實際HTTP可靠地可用的出側向IIS提出請求。即使您將其設置爲靜態字段。你也許可以試着嘲笑本地對該網站的請求來啓動電子郵件。我對ActionMailer一無所知,但也許他們是在控制器之外使用框架的一種方式?

爲了嘲笑一個請求,你需要知道你首先定位的動作的URL,像這樣的東西應該是由開發者配置的設置,或者如果可能的話,由網站自動配置。

 var client = new System.Net.WebClient(); 
     client.DownloadString("http://localhost/Controller/Action"); 

這不是一個推薦的方法,因爲它們可能與開放套接字等的權限有關。到目前爲止,發送電子郵件的最佳方式是使用已存在的框架,如System.Net.Mail.SmtpClient

+0

不,目前沒有真正的方法可以在控制器之外使用他們的框架。我怎麼能這樣嘲笑本地請求? – chobo2 2011-05-20 20:10:48

+0

farmework使用的底層代碼可能是SmtpClient框架正在做的是允許您使用主頁和視圖編寫漂亮的電子郵件。這樣做的缺點是它需要httpcontext,因爲這是asp.net mvc需要的,不幸的是我在沒有實時mvc請求的情況下使用它。 – chobo2 2011-05-20 22:35:50

相關問題