chobo2 chobo2 - 3 months ago 40
C# Question

How to create my own http context for a controller?

I am using mvc 3 and quartz scheduler.

Currently I have this

  1. A job gets created and executed.

  2. Goes to a controller where I map domain to view models with automapper

  3. Results go to controller where I want to use Action Mailer

I get an error

System.ArgumentNullException was unhandled by user code
Message=Value cannot be null.
Parameter name: httpContext
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()

// Job

public void Execute(JobExecutionContext context)

// some code to do some checking and to get results above(not shown)
RemindersController remindersController = new RemindersController();


// controller (to do mapping)\

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 mailer

public EmailResult SendCalendarAppointmentNotifiation(CalendarAppointmentReminderVM vm)
Subject = String.Format("Subject");
return Email("SendCalendarAppointmentEmail", vm);

Dies right on
return Email("SendCalendarAppointmentEmail", vm);
I want to use this as I am using it to send out all my other emails and I find it alot better way to send emails(easier to make them look nicer as you can use master pages and strongly typed views).


The Job execution of Quartz doesn't run in a HttpRequest. Also HttpContext.Current is NULL. It seems that this mailer doesn't work without the request environment (hava a look at StackTrace)

checkout Render a view as a string (render view, put into mail and send..), but unfortunately in this post all samples works with the current ControllerContext/HttpContext too.