Posted: 5/11/2020
During work with our latest project, where we’re using Azure DevOps for CI/CD, I encountered this error while trying to build project which used enum as generic type parameter (C# 7.3 feature). I’ve discovered that is quite common issue, without short and simple explanation, so decided to write a quick blog post on how I dealt with it.
I was really confused - build on my local machine worked fine. I was using .NET Framework 4.7 with Visual Studio 2019. I’ve looked into the documentation provided by Microsoft and found a table presented below:
Target framework | Version | C# language version default |
.NET Core | 3.x | C# 8.0 |
.NET Core | 2.x | C# 7.3 |
.NET Standard | 2.1 | C# 8.0 |
.NET Standard | 2.0 | C# 7.3 |
.NET Standard | 1.x | C# 7.3 |
.NET Framework | all | C# 7.3 |
Default C# language version used by compiler - source.
It made me even more confused. According to this, my build should success - all versions of .NET Framework uses C# 7.3 by default. So why my Azure DevOps build pipeline failed?
As I found out here - Azure DevOps build .Net Core error CS8107 - explanation is simple: Azure DevOps agent uses Dotnet CLI for build process. The above table applies for the compiler that is used by Visual Studio. It looks different for Azure DevOps - Dotnet CLI in DevOps by default uses C# 7.0. This is why build succeed on my local machine, but failed in pipeline.
To force usage of C# 7.3 by Azure DevOps, you must specify this explicitly. To do this, create a file Directory.Build.props in your solution root folder and fill it in with following content:
<Project>
<PropertyGroup>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
</Project>
It does not affect your local build in any way - compiler for .NET Framework (and .NET Core 2.x and 3.x) by default uses C# 7.3 - it’s just the information for Azure DevOps to use proper language version.
And that’s it! After adding the file, the build pipeline finished successfully