Dotnet build specify the main method

I am using dotnet to create a .NET Core C # project from the command line. The project has several classes with the main method. So I get the error:

 $ dotnet build Microsoft (R) Build Engine version 15.1.548.43366 Copyright (C) Microsoft Corporation. All rights reserved. Test.cs(18,28): error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point. Build FAILED. 

Passing the /main switch results in an error:

 $ dotnet build /main:Test Microsoft (R) Build Engine version 15.1.548.43366 Copyright (C) Microsoft Corporation. All rights reserved. MSBUILD : error MSB1001: Unknown switch. Switch: /main:Test 

How to pass the /main option to the dotnet command?

+7
c # msbuild .net-core
source share
1 answer

You can edit your csproj to determine which class to use (inside a PropertyGroup ):

 <StartupObject>foo.Program2</StartupObject> 

or specify this MSBuild property on the command line with:

 $ dotnet build foo.csproj /p:StartupObject=foo.Program2 
+14
source share

All Articles