Andreas Stolpmann
Andreas Stolpmann

Reputation: 41

Console Window shown despite using OutputType WinExe with Microsoft.NET.Sdk.Web in Visual Studio

If i add <OutputType>WinExe</OutputType> to a new ConsoleApp the app gets started, the Main method is run and the app is shut down again.

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
    </PropertyGroup>
</Project>

If i change the Sdk to Microsoft.NET.Sdk.Web and start the app again, a console window is opened.

Why does using <Project Sdk="Microsoft.NET.Sdk.Web"> cause a console window to open? Is there a way to prevent that?

Edit: It seems like the console window is only opened if the app is started in Visual Studio. If i run the exe without Visual Studio the app is run as a background process, as expected.

Edit2: I will try to rephrase the question:

Using <Project Sdk="Microsoft.NET.Sdk.Web"> causes a bunch of props and target files to be included into the project. Somewhere in there must be the reason for the console window to open.

Upvotes: 2

Views: 1357

Answers (2)

Andreas Stolpmann
Andreas Stolpmann

Reputation: 41

I opened an issue on GitHub and got confirmation that this is a bug in visual studio.

See: https://github.com/dotnet/project-system/issues/6613

Upvotes: 2

Mr Qian
Mr Qian

Reputation: 23715

Why does using cause a console window to open? Is there a way to prevent that?

When you change your project to use Microsoft.NET.Sdk.Web which means it is a web project, you did not reload your project correctly. It missed a launchSettings.json file which will specify to start your project as a website.

In my side, if you did not reload your project correctly, it will miss launchSettings.json file, so when you run your project, it will start a console window:

enter image description here

Suggestion

1) Close VS Instance, delete .vs hidden folder under the solution folder, bin and obj folder.

2),restart VS to open your project to reinitialize the project.

It will generate launchSettings.json file.

enter image description here

This is my test result and it will not call a console window.

enter image description here

Besides, if your project already has a launchSettings.json file, you could delete it and then follow step 1 and step 2 to recreate the file.

Upvotes: 0

Related Questions