6
votes

I have a console application which has async calls for e.g. the signature of the main method looks like this

static async Task MainAsync(string[] args)
{

}

I am able to compile the build in my local machine. But I have a VSTS (DevOps Azure) CI/CD pipeline where I am using a custom hosted agent in that machine, over there once the CI executes it gives the error:

##[error]CSC(0,0): Error CS5001: Program does not contain a static 'Main' method suitable for an entry point

3
"I am using a custom hosted agent in that machine". That's not enough. Remember on your machine there can be multiple MSBuild instances, blog.lextudio.com/the-rough-history-of-msbuild-cc72a217fa98 Your pipeline definition must point to the latest MSBuild instace, so as to use the latest C# compiler.Lex Li

3 Answers

5
votes

I got it working by forcing the user agent to use VS2017. Click phase 1 and then change it to the following: HostedVS2017

This forces the user agent to use 2017 which has the latest version of C# instead of 2015 (which it was falling back to for me).

3
votes

Can you try building your local code in release mode and see if you are getting the same issue.

Make sure to add C# 7.1 to any CPU and release property groups.

  • Right-click Your Project, click Properties

  • Click Build if it's not already selected

  • Change Configuration to All Configurations

  • Click Advanced...

  • Change the language version

Refer this issue in github for more details.

https://github.com/dotnet/roslyn/issues/21783

2
votes

https://www.visualstudiogeeks.com/azure/devops/using-latest-version-of-csharp-with-dotnetcore

in pipeline in visual studio build add to MSBuild Arguments

/property:langversion=latest

or

Edit the c# Project and add

<LangVersion>latest</LangVersion>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PackageAction>BuildDefault</PackageAction>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PackageAction>BuildDefault</PackageAction>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>