【问题标题】:How can I configure Visual Studio Code to run/debug .NET (dotnet) Core from the Windows Subsystem for Linux (WSL)?如何配置 Visual Studio Code 以从适用于 Linux 的 Windows 子系统 (WSL) 运行/调试 .NET (dotnet) Core?
【发布时间】:2019-05-16 08:56:03
【问题描述】:

我在Windows Subsystem for Linux (WSL) 中安装了 .NET 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}"
        }
    ]
}

还有我的 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"
        }
    ]
}

我的目录结构:

但是当我点击“开始调试”按钮时,出现以下错误:

启动:程序“不存在

【问题讨论】:

  • 您的 json 中有 cmets,这通常不是一个好主意。您可以删除这些并尝试吗?
  • 哦,我当然可以,但是这些是由 VS Code 添加的,它为这些 json 文件生成了基础。我怀疑它会有所作为。

标签: c# debugging visual-studio-code .net-core windows-subsystem-for-linux


【解决方案1】:

在 GitHub 上有一篇关于该主题的精彩文章 - Windows Subsystem for Linux

长话短说,您需要先在 Windows 10 Creators Update 之后验证您的版本:

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

注意以下事项

如果您已升级到 Windows 创意者更新并且已经拥有 WSL 安装后,您可能仍然在 WSL 中拥有 Ubuntu 14。如果版本 是14,在cmd提示符下运行以下命令重新安装和 更新 WSL。

lxrun /uninstall /full
lxrun /install

下载调试器

sudo apt-get install unzip
curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/vsdbg

调试器将安装在~/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"
           }
       }

请注意:

  • /.vscode/launch.json:这提供了一系列不同的配置,可用于启动应用程序。 Debug 视图中有一个下拉菜单,用于选择哪个配置处于活动状态。
  • /.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"
            ]
        }
    ]
}

请注意

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

  • pipeProgram 设置为 bash.exe

  • debuggerPath 指向 vsdbg,coreclr 调试器。

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

附加的 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
           }
       }

请注意

  • "processId": "${command:pickRemoteProcess}" 列出了使用管道程序在 WSL 上运行的进程。
  • 如果设置为 true,quoteArgs 将使用空格引用任何参数和调试器命令。
  • 使用sourceFileMap 映射源(如果它们在 与建造地点不同的位置。如果你建立你的 Linux 中的项目,请确保添加来自/mnt 驱动器的映射 字母。示例:"sourceFileMap": { "/mnt/c/": "c:\\" }
  • 文件和路径在 Linux 中区分大小写。

【讨论】:

  • lxrun.exe 在最新的 Windows 10 版本中不可用。使用wslconfig.exe /uninstall distro-name
  • 非常感谢,有没有办法摆脱硬编码的/mnt/c/temp/dotnetapps/wslApp 并将其替换为变量或自定义 vscode 命令?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-13
  • 2022-10-16
  • 2019-07-03
相关资源
最近更新 更多