title: Generating/Sending pdf email attachments within a hangfire job
body: <p>In our ASP.NET MVC web application we send emails as part of scheduled tasks handled by Hangfire for which I am using Postal as described <a href="http://docs.hangfire.io/en/latest/tutorials/send-email.html" rel="nofollow noreferrer">here</a>
  The method works fine and we are able to send HTML/text emails. Now we need to generate and attach PDF files as well. The attached PDF needs to be generated dynamically by use of a Razor template. First I tried to use Rotativa in order to generate the PDF. However I encountered the problem that method BuildPdf needs a ControllerContext which is not available in the background HangFire process. I tried to fake the ControllerContext as</p>
  
  <pre><code>using (var memWriter = new StringWriter(sb))
              {
  
                  var fakeResponse = new HttpResponse(memWriter);
  
                  var fakeRequest = new HttpRequest(null, "http://wwww.oururl.com", null);
                  var fakeHttpContext = new HttpContext(fakeRequest, fakeResponse);
                  var emailController = new BackgroundEmailController();
                  var fakeControllerContext = new ControllerContext(new HttpContextWrapper(fakeHttpContext), new RouteData(), emailController);
  
                  var attachment = emailController.BillAttachment(email);
  
                  var pdf = attachment.BuildPdf(fakeControllerContext);
  
                  if (pdf != null &amp;&amp; pdf.Count() &gt; 0)
                  {
                      using (MemoryStream ms = new MemoryStream(pdf))
                      {
                          var contentType = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
                          email.Attach(new System.Net.Mail.Attachment(ms, contentType));
                      }
                  }
              }              
  </code></pre>
  
  <p>However this raised a NullReference error in Rotativa.
   Then I tried first to compile the template view with RazorEngine to HTML(and then convert the HTML to pdf by some mean)  as</p>
  
  <pre><code>var engineService = RazorEngineService.Create();
  
              engineService.AddTemplate(cache_name, File.ReadAllText(billAttachmentTemplatePath));
              engineService.Compile(cache_name, modelType: typeof(BillEmail));
              var html = engineService.Run(cache_name, null, email);
  
              using (var ms = CommonHelper.GenerateStreamFromString(html))
              {
                  var contentType = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Html);
                  email.Attach(new System.Net.Mail.Attachment(ms, contentType));
              }
  </code></pre>
  
  <p>And it throws another NullReference in the RazorEngine dynamic DLL:</p>
  
  <pre><code> System.NullReferenceException: Object reference not set to an instance of an object.
     at CompiledRazorTemplates.Dynamic.RazorEngine_bb2b366aaef64f2bbc2997353f88cc9e.Execute()
     at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context, TextWriter reader)
  </code></pre>
  
  <p>I was wondering if anybody have suggestions for generating PDF files from a template in a Hangfire process?</p>