locked
csproj is not using latest C# version in VS 2019 RRS feed

  • Question

  • User1428199325 posted

    I had upgraded the solution/projects to VS 2019 & .NET Core in all projects to netcoreapp3.1 & netstandard1.2 .  When compiled WebApp csproj is still showing older version because of this compiler error message "Program does not contain a static Main method suitable for an entry point".  I'm using async which is now permitted in C# version 8 & up.  I even try LangVersion as latest or preview w/ no luck, I read documentation that we don't need to specify LangVersion starting with .NET Core 3.0 & up as it will be done automatically.  So what could be the problem here?

    [ Program.cs ]

        public class Program

        {

            public async static void Main(string[] args)

            {

                await Host.CreateDefaultBuilder(args)

                    .ConfigureWebHostDefaults(webBuilder => {

                        webBuilder.UseContentRoot(Directory.GetCurrentDirectory());

                        webBuilder.UseKestrel();

                        webBuilder.UseAzureAppServices();

                        webBuilder.UseStartup<Startup>();

                        //webBuilder.UseSerilog()

                    })

                .ConfigureLogging((logging) =>

                {

                    //logging.AddSerilog();

                })

                .Build()

                .RunAsync();

            }

        }

    [ csproj ]

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

          <PropertyGroup>

            <VersionPrefix>1.2.0</VersionPrefix>

            <OutputType>Exe</OutputType>

            <TargetFramework>netcoreapp3.1</TargetFramework>

            <PreserveCompilationContext>true</PreserveCompilationContext>

            <AssemblyName>ABCoA.Payments.CustomerWeb</AssemblyName>

            <PackageId>ABCoA.Payments.CustomerWeb</PackageId>

          </PropertyGroup>

          <PropertyGroup>

            <LangVersion>preview</LangVersion>

          </PropertyGroup>

        </Project>

    Thursday, May 7, 2020 12:40 PM

Answers

  • User475983607 posted

    I added async to the main method in a new Core 3.1 project without issue.

     

        public class Program
        {
            public static async Task Main(string[] args)
            {
                var host = CreateHostBuilder(args).Build();
    
                await host.RunAsync();
            }
    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                    });
        }
      <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
      </PropertyGroup>

    The latest C# compiler determines a default language version based on your project's target framework or frameworks. Visual Studio doesn't provide a UI to change the value, but you can change it by editing the csproj file.

    I still don't get the benefit of doing this.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2020 1:28 PM

All replies

  • User753101303 posted

    Hi,

    Yes but it is likely done when creating a new project and here it seems an upgrade in which case it is likely left unchanged. Also you have : <LangVersion>preview</LangVersion>

    Try to use whatever you prefer from https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#c-language-version-reference

    Edit: ah you changed to preview and tried a 8.0 earlier ???

    Thursday, May 7, 2020 1:05 PM
  • User475983607 posted

    I can't think of a good reason for a Web Application's entry point to be async.  Can you explain the problem you are trying to solve?

    Thursday, May 7, 2020 1:07 PM
  • User1428199325 posted

    Hi,

    Yes but it is likely done when creating a new project and here it seems an upgrade in which case it is likely left unchanged. Also you have : <LangVersion>preview</LangVersion>

    Try to use whatever you prefer from https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#c-language-version-reference

    Edit: ah you changed to preview and tried a 8.0 earlier ???

    I did create a new Web Project & updated the Main to use async.  It still fail, even though this is a clean seperation from everything else.  I even try 8.0 & latest as you suggested, still same error.

    Thursday, May 7, 2020 1:08 PM
  • User1428199325 posted

    mgebhard

    I can't think of a good reason for a Web Application's entry point to be async.  Can you explain the problem you are trying to solve?

    Here's the example of why, see "RunAsync()" below.  Startting with C# 7.1 & up, the Main can be async now.  We been moving things over to async as well.

        public class Program

        {

            public async static void Main(string[] args)

            {

                await CreateHostBuilder(args).Build().RunAsync();

            }

            public static IHostBuilder CreateHostBuilder(string[] args) =>

                Host.CreateDefaultBuilder(args)

                    .ConfigureWebHostDefaults(webBuilder =>

                    {

                        webBuilder.UseStartup<Startup>();

                    });

        }

    P.S. Why is .NET forum keep adding <div></div> tags here?  How to make it not do that?

    Thursday, May 7, 2020 1:11 PM
  • User475983607 posted

    I added async to the main method in a new Core 3.1 project without issue.

     

        public class Program
        {
            public static async Task Main(string[] args)
            {
                var host = CreateHostBuilder(args).Build();
    
                await host.RunAsync();
            }
    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                    });
        }
      <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
      </PropertyGroup>

    The latest C# compiler determines a default language version based on your project's target framework or frameworks. Visual Studio doesn't provide a UI to change the value, but you can change it by editing the csproj file.

    I still don't get the benefit of doing this.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2020 1:28 PM