ASP.NET MVC: Exception rendering view in new thread ASP.NET MVC: Exception rendering view in new thread asp.net asp.net

ASP.NET MVC: Exception rendering view in new thread


I'm guessing this has to do with the fact that the controller context is no longer valid in the new thread, but I'm not sure.

Precisely.

So, what is the best way to approach this?

Send emails from another process, not your ASP.NET MVC application.

What alternatives do I have?

One possibility is to use RazorEngine:

private void SendMessagesLongRunningProcess(){    var users = GetUsers();    foreach (var user in users)    {        string view = Server.MapPath("~/Views/MailTemplates/SomeTemplate.cshtml");        string template = System.IO.File.ReadAllText(view);        string message = Razor.Parse(template, user);        SendEmail(user.email, message);    }}

You might also checkout MvcMailer and send the email asynchronously (take a look at the Send Email Asynchronously section of the step-by-step-guide).