为什么这个第一次工作正常但不是第二次?

时间:2011-05-20 18:34:37

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

我正在使用一个调度程序,它将通过名为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是   未按用户代码处理
  Message =对象引用未设置为   对象的实例。
  Source = WebDev.WebHost40 StackTrace:          在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()          在System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext   pageContext,TextWriter writer,   WebPageRenderingBase startPage)          在System.Web.Mvc.RazorView.RenderView(ViewContext   viewContext,TextWriter writer,Object   实例)          在System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext   viewContext,TextWriter writer)          在ActionMailer.Net.EmailResult.RenderViewAsString(ControllerContext   上下文,IView视图)          在ActionMailer.Net.EmailResult.AddMessageViews(ControllerContext   上下文)          在ActionMailer.Net.EmailResult.ExecuteResult(ControllerContext   上下文)          在ActionMailer.Net.MailerBase.Email(String   viewName,Object model,String   masterName)          在EmailController.SendCalendarAppointmentNotifiation(CalendarAppointmentReminderVM   vm)在EmailController.cs中:第80行          在RemindersController.CalendarAppointmentsReminders(List`1   约会)   RemindersController.cs:第61行          在AppointmentRemindersJob.Execute(JobExecutionContext   上下文)   AppointmentRemindersJob.cs:第40行          在Quartz.Core.JobRunShell.Run()
  的InnerException:

1 个答案:

答案 0 :(得分:0)

在进入IIS的实际http请求之外,HttpContext不会可靠地可用。即使您将其设置为静态字段。您可以尝试模拟本地对网站发出的请求以启动电子邮件。我对ActionMailer一无所知,但也许他们是一种在控制器之外使用他们的框架的方法?

要模拟请求,您需要首先知道您要定位的操作的网址,这样的内容应该是开发人员配置的设置,或者如果可能的话自动由网站设置。

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

这不是推荐的方法,因为它们可能是权限打开套接字等问题。到目前为止,发送电子邮件的最佳方式是使用已存在的框架,例如System.Net.Mail.SmtpClient