title: RazorLight.Compilation Exception: Failed to compile generated Razor template
body: <p>I'm creating a <code>.NET 5 worker service</code> app that watches few other apps and sends email when required.
  I'm using <a href="https://github.com/lukencode/FluentEmail" rel="nofollow noreferrer">FluentEmail</a> to send those emails and everything works fine, until I deploy the app.</p>
  <p>When I deploy the app and it attempts to send an email, it throws this exception:</p>
  <pre><code>RazorLight.Compilation.TemplateCompilationException: Failed to compile generated Razor template:
  - (3:35) The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (3:10) The type 'Type' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (35:37) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (38:66) 'GeneratedTemplate.ExecuteAsync()': return type must be 'Task' to match overridden member 'TemplatePageBase.ExecuteAsync()'
  - (38:66) The type 'Task' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (40:12) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (41:12) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (42:12) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (43:12) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (53:12) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (69:12) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (84:12) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (44:19) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (92:12) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (47:20) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (100:12) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (50:20) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (108:12) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (53:19) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (116:12) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  - (124:12) The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  See CompilationErrors for detailed information
     at RazorLight.Compilation.RoslynCompilationService.CompileAndEmit(IGeneratedRazorTemplate razorTemplate)
     at RazorLight.Compilation.RazorTemplateCompiler.CompileAndEmit(RazorLightProjectItem projectItem)
     at RazorLight.Compilation.RazorTemplateCompiler.OnCacheMissAsync(String templateKey)
  --- End of stack trace from previous location ---
     at RazorLight.EngineHandler.CompileTemplateAsync(String key)
     at RazorLight.EngineHandler.CompileRenderAsync[T](String key, T model, ExpandoObject viewBag)
     at CallSite.Target(Closure , CallSite , Object )
     at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
     at FluentEmail.Razor.RazorRenderer.ParseAsync[T](String template, T model, Boolean isHtml)
     at FluentEmail.Razor.RazorRenderer.FluentEmail.Core.Interfaces.ITemplateRenderer.Parse[T](String template, T model, Boolean isHtml)
     at FluentEmail.Core.Email.UsingTemplateFromFile[T](String filename, T model, Boolean isHtml)
     at WatchDog.Services.NotificationService.SendEmailAsync[TModel](String subject, String fileName, TModel model) in C:\Users\SomeOne\source\repos\WatchDog\WatchDog\Services\NotificationService.cs:line 41
  </code></pre>
  <p>My setup looks like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Views Folder</th>
  <th>References</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td><img src="https://i.stack.imgur.com/v55J9.png/200" width="300" /></td>
  <td><img src="https://i.stack.imgur.com/RqS1d.png/25" width="200" /></td>
  </tr>
  </tbody>
  </table>
  </div><div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Views Folder</th>
  </tr>
  </thead>
  </table>
  </div>
  <p>I added the Views folder and please note that this is a <code>.NET 5 Worker Service</code> project (if that matters instead of it being some Web project).</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Program.cs file:</th>
  </tr>
  </thead>
  </table>
  </div>
  <p>Inside the ConfigureServices, I've added the <code>RazorRenderer</code> as:</p>
  <pre><code>//Set email service using FluentEmail
   services.AddFluentEmail(&quot;appname@domain.com&quot;)
   .AddRazorRenderer(@$&quot;{Directory.GetCurrentDirectory()}/Views/&quot;)
   .AddSmtpSender(&quot;smtp.somesmtp.com&quot;, 25)
   .AddSmtpSender(new System.Net.Mail.SmtpClient() { });
  </code></pre>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>NotificationService.cs file:</th>
  </tr>
  </thead>
  </table>
  </div>
  <pre><code>private async Task SendEmailAsync&lt;TModel&gt;(string subject, TModel model)
  {
      try
      {
          using (var scope = _serviceProvider.CreateScope())
          {
              var email = await scope.ServiceProvider.GetRequiredService&lt;IFluentEmail&gt;()
                      .To(string.Join(&quot;;&quot;, _emailRecipients))
                      .Subject(subject)
                      .UsingTemplateFromFile(&quot;./Views/Emails/SomeReport.cshtml&quot;, model)
                      .SendAsync();
          }
      }
      catch (Exception ex)
      {
          _logger.LogError(ex, &quot;Failed to send email. Check exception for more information.&quot;);
      }
  }
  </code></pre>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>SomeReport.cshtml file:</th>
  </tr>
  </thead>
  </table>
  </div>
  <p><code>SomeReport.cshtml</code> is inside <code>Views\Emails\SomeReport.cshtml</code> which looks like this:</p>
  <pre><code>@using System.Collections.Generic;
  @using WatchDog.Models;
  
  @model IEnumerable&lt;SomeReport&gt;
  @{
      Layout = &quot;./Shared/_Layout.cshtml&quot;;
  }
  
  @* Work with the Model here... *@
  </code></pre>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>_Layout.cshtml file:</th>
  </tr>
  </thead>
  </table>
  </div>
  <p><code>_Layout.cshtml</code> is inside <code>Views\Shared\_Layout.cshtml</code> which looks like this:</p>
  <pre><code>@* Some common layout styles here *@
  @RenderBody()
  </code></pre>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>WatchDog.csproj file:</th>
  </tr>
  </thead>
  </table>
  </div>
  <p>I've also added <code>PreserveCompilationContext</code> and <code>PreserveCompilationReferences</code>:</p>
  <pre><code>&lt;PropertyGroup&gt;
    &lt;TargetFramework&gt;net5.0&lt;/TargetFramework&gt;
    &lt;PreserveCompilationContext&gt;true&lt;/PreserveCompilationContext&gt;
    &lt;PreserveCompilationReferences&gt;true&lt;/PreserveCompilationReferences&gt;
  &lt;/PropertyGroup&gt;
  </code></pre>
  <p>I've looked at everywhere and still haven't found a solution to this.
  Please help.</p>
  <p>Thanks!</p>