Search code examples
c#asp.net.netazurerazor

FluentEmail - RazorLight error: Failed to compile generated Razor template error


We are using FluentEmail to build emails, the below code runs fine in our local machine, but once deployed through Azure, once it hits the Razor code it seems to error:

Error is:

RazorLight.Compilation.TemplateCompilationException: Failed to compile generated Razor template: - (0:0) Invalid name for a preprocessing symbol; '$(BUILDCONFIGURATION)' is not a valid identifier See CompilationErrors for detailed information at RazorLight.Compilation.RoslynCompilationService.CompileAndEmit(IGeneratedRazorTemplate razorTemplate)

The line it errors on:

subject = await razor.ParseAsync(emailTemplate.EmailSubject, placeholderData);

where subject is a string. When I run it locally pointing to the same db as our server, it runs fine. but on the server it errors there.

Is there something missing in the .csproj? Below is what I have currently:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
    <PackageReference Include="ClosedXML" Version="0.102.1" />
    <PackageReference Include="ExcelDataReader" Version="3.6.0" />
    <PackageReference Include="FluentEmail.Razor" Version="3.0.2" />
    <PackageReference Include="FluentEmail.Smtp" Version="3.0.2" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    <PackageReference Include="SendGrid" Version="9.28.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\MyProject.Domain\MyProject.Domain.csproj" />
  </ItemGroup>

</Project>

Solution

  • We found the solution, it was related to the Azure Pipeline variables. The environment in which it worked had the BuildConfiguration and BuildPlatform variables, which were missing in the other environments. Once we added them the code ran fine.