Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the default file(starting point) in dot net core

Tags:

c#

.net

.net-core

I am trying to explore the dot net core functionality to get better understanding on it, for the same i executed

dotnet new
dotnet build
dotnet run

command in command prompt window, it created a project for me and file with name Project.cs has been created and at last it showed Hello World! in the window.

Now i added one more file in the same folder structure with different name SampleDotNetCoreApp.cs, i want to know how i can set SampleDotNetCoreApp.cs as the default starting point of the program execution when i will execute dotnet run command.

In other words i want to know how i can change the starting execution point in dot net core when i have multiple cs file in the same folder.

like image 342
Nishant Kumar Avatar asked Oct 12 '16 09:10

Nishant Kumar


2 Answers

You can edit the "Project.csproj" file to specify which Main method is used

<PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <StartupObject>Project.SampleDotNetCoreApp</StartupObject>
</PropertyGroup>

Note the StartupObject tag, identifying the class to start the program with. This class must have a static Main(string[] args) method.

Use dotnet restore to ensure the changes are saved successfully, then build / run the project

dotnet run and dotnet build do perform an automatic restore, so doing this manually is not necessary

- Yes, I know I'm late to the party, but I just encountered this issue and found it hard to solve. Might as well share my newfound wisdom.

like image 192
Nightmarlin Avatar answered Oct 14 '22 22:10

Nightmarlin


The proper way to address this - without removing code making sure that you only have one main method, and at least given:

.NET Command Line Tools (1.1.6)

with Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core

is to invoke the build with the following arguments:

dotnet build /property:StartupObject=namespace.ClassWithMain

It is very confusing that the error message points you to a /main which is not supported at all - I eventually found out the right property to use (note, I had used /property:main or /property:Main to no avail) from this answer here: dotnet build specify main method

Hope it helps

like image 42
João Antunes Avatar answered Oct 14 '22 22:10

João Antunes