如何在Linux(ubuntu)中使用VSCode调试ASP.NET Core 3.1?

5

我按照默认步骤进行如下操作:

  • 安装 .NET Core (linux-package-manager-ubuntu-1604)
  • 在 VSCode 中创建并打开(ASP.NET Core Empty)项目(cd ~/projects && dotnet new web -o my-api && cd my-api && code .)
  • 添加 vscode tasks.json 和 launch.json(C# Extension)(ms-dotnettools.csharp)
  • 添加断点(在VSCode中)
  • 启动调试(在VSCode中按F5)

但这不会启动Web服务器,也不会在断点处停止... 它确实构建了该项目,因为我可以在 bin/Debug/netcoreapp3.1/ 中看到生成的文件。

在项目根目录运行 dotnet run 命令,即可构建(恢复)和运行 Web 服务器,并且我可以浏览https://localhost:5001http://localhost:5000。但不能进行调试...

这是 VSCode 创建的文件列表

launch.json
{
    // Use IntelliSense to find out which attributes exist for C# debugging
    // Use hover for the description of the existing attributes
    // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/my-api.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
            "serverReadyAction": {
                "action": "openExternally",
                "pattern": "^\\s*Now listening on:\\s+(https?://\\S+)"
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "sourceFileMap": {
                "/Views": "${workspaceFolder}/Views"
            }
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/my-api.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "publish",
            "command": "dotnet",
            "type": "process",
            "args": [
                "publish",
                "${workspaceFolder}/my-api.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "watch",
            "command": "dotnet",
            "type": "process",
            "args": [
                "watch",
                "run",
                "${workspaceFolder}/my-api.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        }
    ]
}

F5 首先显示 Terminal 标签,然后切换到 Debug Console 标签在 VSCode 中。

Terminal-tab
> Executing task: dotnet build /home/user/projects/my-api/my-api.csproj /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary <

Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 76,69 ms for /home/user/projects/my-api/my-api.csproj.
  my-api -> /home/user/projects/my-api/bin/Debug/netcoreapp3.1/my-api.dll

Terminal will be reused by tasks, press any key to close it.

Debug Console
-------------------------------------------------------------------
You may only use the Microsoft .NET Core Debugger (vsdbg) with
Visual Studio Code, Visual Studio or Visual Studio for Mac software
to help you develop and test your applications.
-------------------------------------------------------------------

我在Regolith Linux 1.5 (基于Ubuntu 20.04)下遇到了同样的问题,这是在重新安装后发生的,之前它是可以工作的。如果我找到解决方案,我会及时更新您。 - MathieuAuclair
你在安装 dotnet-sdk 时安装了哪个版本的 libcu? - MathieuAuclair
你能让它工作吗? - MathieuAuclair
1
不,我已经移除了.Net,因为我转向了其他东西。一旦我在Linux机器上重新安装了.Net,我会记录我的安装/使用进度。 - Aytacworld
2个回答

2

看起来这是VS Code安装本身的问题,我只需重新安装VS Code即可解决此问题。

sudo snap remove code
sudo snap install code --classic

尽管这种解决方案不尽如人意,但我有一个猜想,可以解释为什么会发生这种情况。
我使用CLI安装了我的模块,而不是使用GUI。我之所以这样说,是因为在运行我的Web应用程序时,我看到了安装日志。我猜测调试器的安装可能没有在我运行应用程序时完成。这可能导致了包损坏。但是,仅卸载扩展并不能解决问题,所以我不确定发生了什么。
code --install-extension ms-dotnettools.csharp

如果重新安装无法解决问题,请告诉我,并尝试更改您的launch.json,将openExternally替换为debugWithChrome。我在重新安装VS Code的同时更改了设置。

-1

我曾经遇到过同样的问题。

首先尝试重启系统,看看是否能解决问题。 否则,请尝试MathieuAuclair的解决方案,然后重新启动系统。

这对我起作用了。


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