无法从 vscode 中的 .net-core 控制台应用程序读取输入

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

我一直在尝试让 dotnet new console 示例项目(针对 vscode)在 Ubuntu 17.10 中运行。

我可以得到默认程序运行:

using System;

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

但是当我将其更改为也读取输入时,它变得非常不稳定...

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
        }
    }
}

程序生成,但不会打印 Name:。但是,如果我在第 1、2 和 3 行放置断点,我可以看到程序 运行 遍历了所有断点,但没有打印任何内容。直到我停止调试。然后它打印

Name:

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

这里发生了什么?我猜它是一个 vscode 的东西,因为它在 运行 从终端使用 dotnet run.

时按预期工作

Documentation 声明如下:

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

我找到了问题的解决方案

来自链接文档的以下引述还指出,将控制台 属性 从 launch.json 更改为“externalTerminal”或“integratedTerminal "会有所帮助。

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.

正确 - 'internalConsole' 不适用于需要控制台输入的程序。这是官方文档:https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window