c# - 如何为 Controller 创建我自己的 http 上下文?

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

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

目前我有这个

  1. 创建并执行作业。
  2. 转到我映射域的 Controller 以使用自动映射器查看模型
  3. 结果转到我要使用的 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。

关于c# - 如何为 Controller 创建我自己的 http 上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6049930/

相关文章:

c# - 网格线后面的 OxyPlot AreaSeries

javascript - 为什么 WCF 服务无法在托管服务器上运行?

c# - 如何从具有 Array 属性的 Action Method 返回 Json?

c# - 意外的 token 非法 - 正则表达式 - MVC 3 JS

c# - Marshal C++ float to C# float accuracy 问题

c# - 按 Enter 键时出错,C# 控制台

c# - 如何查找具有与另一个集合中的任何值匹配的属性的所有对象?

javascript - 使用 json 将对象数组传递给 mvc Controller

c# - 使用自定义表的成员身份和角色身份验证

asp.net-mvc-3 - EF 4.1-模型关系