如何配置Visual Studio代码以从Windows的Linux子系统(WSL)运行/调试.NET(dotnet)Core?

Cap*_*ack 10 c# debugging .net-core visual-studio-code windows-subsystem-for-linux

我已经在Windows子系统Linux(WSL)中安装了dotnet core 2.2并创建了一个新项目.我还为Visual Studio Code安装了C#扩展,语法高亮显示和IntelliSense似乎正在工作.

但是,当我尝试使用调试器时,事情就停止了.这是我尝试配置它的一步一步.

这是我的launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/bin/Debug/netcoreapp2.2/CodeCore.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "console": "internalConsole",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart"
        },
        {
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/bin/Debug/netcoreapp2.2/CodeCore.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart",
            "launchBrowser": {
                "enabled": true,
                "args": "${auto-detect-url}",
                "windows": {
                    "command": "cmd.exe",
                    "args": "/C start ${auto-detect-url}"
                },
                "osx": {
                    "command": "open"
                },
                "linux": {
                    "command": "xdg-open"
                }
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "sourceFileMap": {
                "/Views": "${workspaceFolder}/Views"
            }
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

和我的tasks.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet build",
            "type": "shell",
            "group": "build",
            "presentation": {
                "reveal": "silent"
            },
            "problemMatcher": "$msCompile"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我的目录结构:

在此输入图像描述

但是当我单击"开始调试"按钮时,我收到以下错误:

launch: program " does not exist
Run Code Online (Sandbox Code Playgroud)

Bar*_*r J 7

GitHub 上有一篇关于该主题的精彩文章 -适用于 Linux 的 Windows 子系统

长话短说,您需要在 Windows 10创意者更新之后首先验证您的版本:

~$ cat /etc/os-release  | grep  -i version
VERSION="16.04.2 LTS (Xenial Xerus)"
VERSION_ID="16.04"
VERSION_CODENAME=xenial
Run Code Online (Sandbox Code Playgroud)

请注意以下几点

如果您已升级到 Windows Creators 更新并且已经安装了 WSL,则 WSL 中可能仍然有 Ubuntu 14。如果版本为 14,请在 cmd 提示符下运行以下命令以重新安装和更新 WSL。

lxrun /uninstall /full
lxrun /install
Run Code Online (Sandbox Code Playgroud)

下载调试器

~$ cat /etc/os-release  | grep  -i version
VERSION="16.04.2 LTS (Xenial Xerus)"
VERSION_ID="16.04"
VERSION_CODENAME=xenial
Run Code Online (Sandbox Code Playgroud)

调试器将安装在~/vsdbg/vsdbg. 它是debuggerPath

用于启动的示例 launch.json 配置

  {
           "name": ".NET Core WSL Launch",
           "type": "coreclr",
           "request": "launch",
           "preLaunchTask": "publish",
           "program": "/mnt/c/temp/dotnetapps/wslApp/bin/publish/wslApp.dll",
           "args": [],
           "cwd": "/mnt/c/temp/dotnetapps/wslApp",
           "stopAtEntry": false,
           "console": "internalConsole",
           "pipeTransport": {
               "pipeCwd": "${workspaceRoot}",
               "pipeProgram": "bash.exe",
               "pipeArgs": [ "-c" ],
               "debuggerPath": "~/vsdbg/vsdbg"
           }
       }
Run Code Online (Sandbox Code Playgroud)

请注意:

  • /.vscode/launch.json:这提供了可用于启动应用程序的一系列不同配置。Debug 视图中有一个下拉菜单,用于选择哪个配置处于活动状态。
  • <your-open-folder>/.vscode/tasks.json:这提供了一系列不同的任务,例如构建您的应用程序,您可以执行这些任务。调试配置可以通过 preLaunchTask 属性链接到这些任务之一。

tasks.json 的 示例“发布”任务(启动所需)

{
    "version": "2.0.0",
    "tasks": [
        ...,
        {
            "label": "publish",
            "command": "dotnet",
            "type": "process",
            "args": [
                "publish",
                "${workspaceFolder}/wslApp.csproj",
                "-o",
                "${workspaceFolder}/bin/publish"
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

请注意

  • preLaunchTask 执行dotnet publish,它在 Windows 上构建项目。由于 coreclr 是跨平台的,二进制文件可以在 WSL 上执行而无需任何额外工作。

  • pipeProgram 设置为bash.exe

  • debuggerPath 指向 vsdbg,coreclr 调试器。

  • 这将不支持想要从控制台读取的程序。

attach 的示例 launch.json 配置

 {
           "name": ".NET Core WSL Attach",
           "type": "coreclr",
           "request": "attach",
           "processId": "${command:pickRemoteProcess}",
           "pipeTransport": {
               "pipeCwd": "${workspaceRoot}",
               "pipeProgram": "bash.exe",
               "pipeArgs": [ "-c" ],
               "debuggerPath": "~/vsdbg/vsdbg",
               "quoteArgs": true
           }
       }
Run Code Online (Sandbox Code Playgroud)

请注意

  • "processId": "${command:pickRemoteProcess}" 使用管道程序列出在 WSL 上运行的进程。
  • quoteArgs 如果设置为 true,将引用带空格的任何参数和调试器命令。
  • 使用sourceFileMap,如果他们在不同的位置比他们建在哪里都可以映射来源。如果您在 Linux 中构建项目,请确保从/mnt驱动器号添加映射。例子:"sourceFileMap": { "/mnt/c/": "c:\\" }
  • 在 Linux 中文件和路径区分大小写。

  • `lxrun.exe` 在最新的 Windows 10 版本中不可用。使用`wslconfig.exe /uninstall distro-name`。 (2认同)