简体   繁体   English

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

[英]How can I configure Visual Studio Code to run/debug .NET (dotnet) Core from the Windows Subsystem for Linux (WSL)?

I've installed dotnet core 2.2 in the Windows Subsystem for Linux (WSL) and created a new project. 我已经在Windows子系统Linux(WSL)中安装了dotnet core 2.2并创建了一个新项目。 I've also installed the C# extension for Visual Studio Code and the syntax highlighting and IntelliSense seems to be working. 我还安装了Visual Studio Code的C#扩展,语法高亮和IntelliSense似乎正在工作。

However, when I try to use the debugger things stop working. 但是,当我尝试使用调试器时,事情就停止了。 Here's a step by step of what I've tried to do to configure it. 这是我尝试配置它的一步一步。

Here's my launch.json: 这是我的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}"
        }
    ]
}

And my tasks.json: 和我的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"
        }
    ]
}

My directory structure: 我的目录结构:

在此输入图像描述

But when I click the "Start Debugging" button I get the following error: 但是当我单击“开始调试”按钮时,我收到以下错误:

launch: program " does not exist

There is a great article on github regarding the subject - Windows Subsystem for Linux . 关于这个主题的github上有一篇很棒的文章 - 适用于Linux的Windows子系统

To cut a long story short, you need to first verify your version after windows 10 creators update: 简而言之,您需要在Windows 10创建者更新后首先验证您的版本:

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

Notice the following : 请注意以下事项

If you had upgraded to Windows Creators update and already had WSL installed, you might still have Ubuntu 14 in the WSL. 如果您已升级到Windows Creators更新并且已经安装了WSL,那么您可能仍然在WSL中安装了Ubuntu 14。 If the version is 14, run the following commands in a cmd prompt to reinstall and update WSL. 如果版本为14,请在cmd提示符中运行以下命令以重新安装和更新WSL。

lxrun /uninstall /full
lxrun /install

Download the debugger : 下载调试器

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

Debugger will be installed at ~/vsdbg/vsdbg , it is the debuggerPath . 调试器将安装在~/vsdbg/vsdbg ,它是debuggerPath

Sample launch.json configuration for launch : 发布示例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"
           }
       }

Please note: 请注意:

  • /.vscode/launch.json: This provides an array of different configurations you can use to launch your application. /.vscode/launch.json:这提供了一组可用于启动应用程序的不同配置。 There is a drop down in the Debug view for selecting which configuration is active. Debug视图中有一个下拉列表,用于选择哪个配置处于活动状态。
  • /.vscode/tasks.json: This provides an array of different tasks, like building your application, that you can execute. /.vscode/tasks.json:这提供了一系列不同的任务,比如构建应用程序,可以执行。 Debug configurations can link to one of these tasks through the preLaunchTask property. 调试配置可以通过preLaunchTask属性链接到其中一个任务。

Sample 'publish' task for tasks.json (needed for launching) : tasks.json的示例'publish'任务(启动时需要)

{
    "version": "2.0.0",
    "tasks": [
        ...,
        {
            "label": "publish",
            "command": "dotnet",
            "type": "process",
            "args": [
                "publish",
                "${workspaceFolder}/wslApp.csproj",
                "-o",
                "${workspaceFolder}/bin/publish"
            ]
        }
    ]
}

Please Note : 请注意

  • preLaunchTask executes dotnet publish, which builds the project on Windows. preLaunchTask执行dotnet发布,它在Windows上构建项目。 Since coreclr is cross-platform, the binary can be executed on WSL without any extra work. 由于coreclr是跨平台的,因此二进制文件可以在WSL上执行而无需任何额外的工作。

  • pipeProgram is set to bash.exe. pipeProgram设置为bash.exe。

  • debuggerPath points to vsdbg, the coreclr debugger. debuggerPath指向coreclr调试器vsdbg。

  • This will not support programs that want to read from the console. 这不支持想要从控制台读取的程序。

Sample launch.json configuration for 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
           }
       }

Please Note : 请注意

  • "processId": "${command:pickRemoteProcess}" lists the processes running on WSL using the pipe program. "processId": "${command:pickRemoteProcess}"使用管道程序列出在WSL上运行的进程。
  • quoteArgs will quote any arguments and debugger commands with spaces if set to true. 如果设置为true, quoteArgs将引用带空格的任何参数和调试器命令。
  • Use sourceFileMap to map sources if they are available in a different location than where they were built. 如果源可用于与构建它们的位置不同的位置,请使用sourceFileMap来映射源。 If you build your project in Linux, make sure to add a map from the /mnt drive letters. 如果在Linux中构建项目,请确保从/mnt驱动器号添加地图。 Example: "sourceFileMap": { "/mnt/c/": "c:\\\\" } 示例: "sourceFileMap": { "/mnt/c/": "c:\\\\" }
  • File and paths are case sensitive in Linux. Linux中的文件和路径区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 linux/ubuntu 中为 dotnet core (.NET 6) 配置 windows 身份验证 - How to configure windows authentication in linux/ubuntu for dotnet core (.NET 6) ASP.Net Core应用程序可在Visual Studio中运行,但不能与dotnet运行 - ASP.Net Core app works in visual studio but not with dotnet run 在调试中为SSL配置launchSettings.json - ASP.NET Core / Visual Studio Code - Configure launchSettings.json for SSL in debug - ASP.NET Core / Visual Studio Code 如何让我的 Python 脚本从我的 dotnet 核心 windows 服务运行? - How can I get my Python script to run from my dotnet core windows service? 如何在 .Net Core 3、Visual Studio 2019 和 docker 中使用“dotnet watch run” - How to use "dotnet watch run" with .Net Core 3, Visual Studio 2019 and docker Visual Studio Code:如何配置“dotnet”在编译时不显示警告 - Visual Studio Code: How to configure 'dotnet' not to show warnings when compiling 使用 dotnet run 时如何调试 .NET 核心控制台应用程序 - How to Debug a .NET Core Console App When Using dotnet run Dotnet手表与调试Visual Studio代码 - Dotnet watch with debug Visual Studio Code 如何从Visual Studio以调试模式运行NUnit? - How do I run NUnit in debug mode from Visual Studio? 如何使用Visual Studio EXPRESS调试本机代码 - How can I debug native code using Visual Studio EXPRESS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM