Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failing to read input from .net-core console application in vscode

I've been trying to get dotnet new console example project (for vscode) to work in Ubuntu 17.10.

I can get the default program to run:

using System;

namespace dotnet_console
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello world!");
        }
    }
}

But when i change it to read input as well, it gets really wonky...

using System;

namespace dotnet_console
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Name: "); // 1
            var name = Console.ReadLine(); // 2
            Console.WriteLine("Hello {0}!", name); // 3
        }
    }
}

The program builds, but it won't print Name:. However if i put breakpoints on line 1, 2 & 3, i can see that the program runs through ALL of them, but nothing prints. That is until i stop the debugging. Then it prints

Name:

The program '[16322] dotnet-console.dll' has exited with code 0 (0x0).

What is happening here? I'm guessing its a vscode thing, because it works as expected when ran from the terminal using dotnet run.

like image 911
Olian04 Avatar asked Feb 10 '18 14:02

Olian04


People also ask

How do I run the console app in VS code?

Switch over to Visual Studio Code Navigate to your Console Project and open it in Visual Studio Code or you can simply type “code .” if your already inside the directory that you want opened. You can run the app by pressing CMD-Shift-P and selecting “Run”. This will open a terminal window that displays “Hello World”.

Does VS code support .NET core?

Visual Studio Code lets you write ASP.NET Core applications by leveraging all of the evolved editing features available to C# and to the other file types in the project. Being a cross-platform itself, it's the perfect companion to start writing MVC applications for Linux, OS X and Windows.

How do I open net core solution in VS code?

Create an ASP.NET Core application Open that empty directory in VS Code by selecting File -> Open Folder. Open the terminal by using the shortcut Ctrl + Shift + ` or Terminal -> New Terminal. Then, it will show you the list of ASP.NET project templates. Select the web application option.

How do you Debug a .NET core console app?

Start debuggingOpen the Debug view by selecting the Debugging icon on the left side menu. Select the green arrow at the top of the pane, next to . NET Core Launch (console). Other ways to start the program in debugging mode are by pressing F5 or choosing Run > Start Debugging from the menu.


2 Answers

The Documentation states the following:

By default, processes are launched with their console output (stdout/stderr) going to the VS Code Debugger Console. This is useful for executables that take their input from the network, files, etc. But this does NOT work for applications that want to read from the console (ex: Console.ReadLine). For these applications, use a setting such as the following

I found a solution for the problem here.

And the following Quote from the linked Documentation also states that changing the console property from the launch.json to either "externalTerminal" or "integratedTerminal "is going to help.

When this is set to externalTerminal the target process will run in a separate terminal.

When this is set to integratedTerminal the target process will run inside VS Code's integrated terminal. Click the 'Terminal' tab in the tab group beneath the editor to interact with your application.

Location of launch.json

like image 72
Tobias Theel Avatar answered Oct 09 '22 03:10

Tobias Theel


Correct - 'internalConsole' is not meant for programs that want to take console input. Here is the official documentation: https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window

like image 44
Gregg Miskelly Avatar answered Oct 09 '22 03:10

Gregg Miskelly