我该如何配置Visual Studio Code以在Windows子系统Linux(WSL)上运行/调试.NET(dotnet)Core?

12
我已经在Windows子系统 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"
        }
    ]
}

我的目录结构:

Enter image description here

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

启动:程序“{{}}”不存在


1
你的json对象内有注释,通常情况下这不是一个好的做法。你能否去掉它们然后再试一下? - Alexandre Elshobokshy
当然,我可以这样做,但那些是由VS Code添加的,它生成了这些json文件的基础。我怀疑这会有什么影响。 - Captain Stack
1个回答

7

关于这个主题,在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 Creators更新并已安装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。 < p >启动的示例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:这提供了一系列不同的配置,您可以使用它们来启动应用程序。在调试视图中有一个下拉菜单,用于选择哪个配置是活动的。
  • <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"
            ]
        }
    ]
}

请注意:
- 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
           }
       }

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

2
在最新的Windows 10版本中,lxrun.exe不可用。请使用wslconfig.exe /uninstall distro-name - Biswapriyo
1
非常感谢您的帮助,有没有一种方法可以摆脱硬编码的路径“/mnt/c/temp/dotnetapps/wslApp”,并用变量或自定义 Visual Studio Code 命令替换它? - matthid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接