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.
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.
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'
It's worth noting that:
dotnet build
on my local machine.DotNetCoreCLI@2
task with a VSBuild@1
task, everything also builds fine.Given that, this issue only occurs when the project meets all of the following conditions:
Microsoft.NET.Sdk.Web
or the Microsoft.NET.Sdk.Razor
SDK.TargetFramework
of either netcoreapp3.0
or netcoreapp3.1
.LangVersion
of 9.0
.DotNetCoreCLI@2
task.<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?
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.
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
Even though .NET 5 is released, you still must set includePreviewVersions
currently.
The .NET 5 SDK can still build the project, despite it being an ASP.NET Core 3.x project.
Other tasks in your pipeline (such as gitversion
, in my case) may explicitly depend on the .NET Core 3.x SDK; if so, you'll need to install that as well:
steps:
- task: UseDotNet@2
displayName: 'Use .NET Core SDK 3.x'
inputs:
version: 3.x
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.