asp.net-coreazure-pipelinesasp.net-core-3.1razorgeneratorc#-9.0

Azure DevOps: “rzc generate exited with code 2”


I have an ASP.NET Core 3.x project that is built and deployed using Azure DevOps Pipelines. After upgrading the project to use C# 9.0—but not ASP.NET Core 5.0—my dotnet build (DotNetCoreCLI@2) task warns:

Unknown C# language version 9.0.

Error

Immediately after the warning, the task fails with the following error:

##[error]C:\Program Files\dotnet\sdk\3.1.404\Sdks\Microsoft.NET.Sdk.Razor\build\netstandard2.0\Microsoft.NET.Sdk.Razor.CodeGeneration.targets(150,5): Error : rzc generate exited with code 2.

Task

There's nothing particularly special about my build task, but for completeness:

- task: DotNetCoreCLI@2
  displayName: 'dotnet build'
  inputs:
    projects: '*.sln'
    arguments: '/p:configuration=$(BuildConfiguration) --no-restore'

Conditions

It's worth noting that:

Given that, this issue only occurs when the project meets all of the following conditions:

<Project Sdk="Microsoft.NET.Sdk.Razor">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <LangVersion>9.0</LangVersion>
  </PropertyGroup>
</Project>

Any idea on how to resolve this issue?


Solution

  • This issue isn't specific to Azure DevOps Pipelines. Instead, the issue appears to be due to an incompatibility between the Razor compiler built into the .NET 3.x SDK and C# 9.0. This necessitates the availability of the .NET 5.x SDK in order to compile a project with Razor views and C# 9.0—even if that project itself is a .NET 3.x project.

    Azure DevOps Pipelines

    As this question is specifically about Azure DevOps Pipelines, this can be resolved by explicitly adding the Use .NET Core SDK task to your pipeline and configuring it use the .NET 5.x SDK:

    - task: UseDotNet@2
      displayName: 'Use .NET Core SDK 5.x'
      inputs:
        version: 5.x
        includePreviewVersions: true
    

    Notes

    This step is probably obvious if you've upgraded your project to use .NET 5.0 and ASP.NET Core 5.0, but may not be obvious if you're still using .NET 3.x and have only upgraded to C# 9.0—and, in fact, it still isn’t necessary if your .NET project doesn’t need to compile Razor.