Error: Feature ‘async main’ is not available in C# 7.0. Please use lang version 7.1 or greater

Today in this article, we will cover below aspects,

Issue Description

.NET/.NET Core build (MSBuild or Visual Studio) gives the below or similar format of the error.

CS8107: "Feature 'async main' is not available in C# 7.0"
CS5001 Program does not contain a static 'Main' method suitable for an entry point 
Resolved Feature Async main is not available in C 70

The issue is most visible in .NET/.NET Core framework using the C# language version for the latest available feature. You should use the right version of the C# language framework to work with those new features.

Resolution

The issue can be resolved using any of the below approaches discussed.

Please note that the C# compiler determines the language version(default) based on the project’s target framework.

Please note that the target project when built, gets the highest compatible language version by default as per the versioning semantics.

If using .NET or .NET Core then this framework will always target only a single framework which will also be a default version.

This versioning could vary from machine to machine depending on the target framework version installed.

Please use any of the below approaches to fix the issue.

Specify Version in Project

Please set the language version in your project file.

Please explicitly instruct the compiler that you would like to use the specific version of the language.

Use the Latest Language version




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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <LangVersion>Latest</LangVersion>
  </PropertyGroup>

</Project>




If you are targetting any preview version framework then the language version can be a target as a preview.




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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <LangVersion>preview</LangVersion>
  </PropertyGroup>

</Project>

One can also target a Specific version by the explicit declaration for  Example: ‘8.0’ as shown below,





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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <LangVersion>8.0</LangVersion>
  </PropertyGroup>

</Project>

Note: If reload the project if an update to the project setting doesn’t work

MSBuild Build command

If using a build server to build the project using the MSBuild command as below,

msbuild /property:langversion=latest

Resolved Feature Async main is not available in C 70

Build props file

You can target multiple projects to use the specific or preview or latest version using Directory.Build.props file.

Please update the file below with the source repository.

 <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <LangVersion>Latest</LangVersion>
  </PropertyGroup>

As a good practice, one can always use the latest version

I have discussed the same in detail on How to Enable C# Language Versioning – Guidelines

References :

Did the above steps resolve your issue? Please sound off in your comments below!

Summary

In this post, we saw how to target the specific version of C# when building .NET Core applications and resolve common issues related to a specific version.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *