gpt4 book ai didi

c# - 如何配置 Visual Studio Code 以从适用于 Linux 的 Windows 子系统 (WSL) 运行/调试 .NET (dotnet) Core?

转载 作者:可可西里 更新时间:2023-11-01 09:13:27 24 4
gpt4 key购买 nike

我在 Windows Subsystem for Linux 中安装了 .NET Core 2.2 (WSL) 并创建了一个新项目。我还为 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

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

launch: program " does not exist

最佳答案

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

注意以下事项:

If you had upgraded to Windows Creators update and already had WSLinstalled, you might still have Ubuntu 14 in the WSL. If the versionis 14, run the following commands in a cmd prompt to reinstall andupdate 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:这提供了一系列不同的配置,您可以使用这些配置来启动您的应用程序。调试 View 中有一个下拉菜单,用于选择激活的配置。
  • /.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 上运行的进程。
  • quoteArgs 如果设置为 true,将用空格引用任何参数和调试器命令。
  • 使用 sourceFileMap 来映射资源,如果它们在与 build 地点不同的地点。如果你建立你的在 Linux 中的项目,确保从 /mnt 驱动器添加 map 字母。示例:"sourceFileMap": { "/mnt/c/": "c:\\"}
  • 文件和路径在 Linux 中区分大小写。

关于c# - 如何配置 Visual Studio Code 以从适用于 Linux 的 Windows 子系统 (WSL) 运行/调试 .NET (dotnet) Core?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53790697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com