in an asp.net mvc / sql project I’v added hangfire following the asp.net mvc tutorial (http://hangfire.readthedocs.org/en/latest/tutorials/send-email.html). I’m using RazorEngine v3.4.2 (there are two RazorEngine projects on NuGet).
My emails are enqueued alright but I get a “RazorEngine.Templating.TemplateCompilationException” (see below).
Code to enqueue mail:
Hangfire.BackgroundJob.Enqueue(() => SendSurveyResponse(userEmail, lFromEmail);
public static void SendSurveyResponse(string userEmail, string lFromEmail)
{
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
var engines = new ViewEngineCollection();
engines.Add(new Postal.FileSystemRazorViewEngine(viewsPath));
var emailService = new Postal.EmailService(engines);
var email = new ViewModels.Email.ResponseAttachedEmail
{
ToEmail = userEmail,
FromEmail = lFromEmail,
Subject = "Survey response",
};
emailService.Send(email);
}
RazorEngine.Templating.TemplateCompilationException
Unable to compile template. Assuming assembly reference 'System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' matches 'System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35', you may need to supply runtime policy Other compilation errors may have occurred. Check the Errors property for more information.
RazorEngine.Templating.TemplateCompilationException: Unable to compile template. Assuming assembly reference 'System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' matches 'System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35', you may need to supply runtime policy
Other compilation errors may have occurred. Check the Errors property for more information.
at RazorEngine.Compilation.DirectCompilerServiceBase.CompileType(TypeContext context) in c:\_git\RazorEngine\src\Core\RazorEngine.Core\Compilation\DirectCompilerServiceBase.cs:line 106
at RazorEngine.Templating.TemplateService.CreateTemplateType(String razorTemplate, Type modelType) in c:\_git\RazorEngine\src\Core\RazorEngine.Core\Templating\TemplateService.cs:line 275
at RazorEngine.Templating.TemplateService.GetTemplate[T](String razorTemplate, Object model, String cacheName) in c:\_git\RazorEngine\src\Core\RazorEngine.Core\Templating\TemplateService.cs:line 393
at RazorEngine.Templating.TemplateService.GetTemplate(String razorTemplate, Object model, String cacheName) in c:\_git\RazorEngine\src\Core\RazorEngine.Core\Templating\TemplateService.cs:line 371
at RazorEngine.Templating.TemplateService.Parse(String razorTemplate, Object model, DynamicViewBag viewBag, String cacheName) in c:\_git\RazorEngine\src\Core\RazorEngine.Core\Templating\TemplateService.cs:line 456
at Postal.FileSystemRazorView.Render(ViewContext viewContext, TextWriter writer)
at Postal.EmailViewRenderer.RenderView(IView view, ViewDataDictionary viewData, ControllerContext controllerContext, ImageEmbedder imageEmbedder)
at Postal.EmailViewRenderer.Render(Email email, String viewName)
at Postal.EmailService.Send(Email email)
at MyTestProject.Web.Controllers.AccountController.SendSurveyResponse(String userEmail, String lFromEmail, String lToFirstName, Int32 lResponseId, DateTime lResponseBegin, String lRespondent, String lStore, String lPathToFile) in c:\Projects\MyTestProject\MyTestProject.Web\Controllers\AccountController.cs:line 617
If I omit the RazorEngine emails are enqueue and handled by the hangfire server and emails are sent off.
public static void SendSurveyResponse(string userEmail, string lFromEmail)
{
var email = new ViewModels.Email.ResponseAttachedEmail
{
ToEmail = userEmail,
FromEmail = lFromEmail,
Subject = "Survey response",
};
email.Send();
}
/Morten