使用最新的 VS Code + C# 开发工具包“找不到可启动的目标”

Jos*_*rez 5 c# visual-studio-code

自从“C# Dev Kit”出现后,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": "C#: DbRefs Debug",
            "type": "dotnet",
            "request": "launch",
            "projectPath": "${workspaceFolder}/DbRefs.csproj"
        }

    ]
}
Run Code Online (Sandbox Code Playgroud)

显然,现在我们的目的是“启动”一个项目文件,而不是通过附加到已编译的 DLL 进行调试,无论这意味着什么。

好吧,我很想尝试一下这个新东西,但它对我来说永远不起作用。100% 的情况下,无论是现有项目还是新的 Hello World 项目,我总是收到错误消息“xxx.csproj 不支持调试。找不到可启动的目标。”。

有什么指点吗?

小智 6

有一条评论告诉你用 IntelliSense 检查配置

你要做的就是创建一个自定义的 C# launch.json 文件

{
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/bin/Debug/<Target, like net6.0>/<YourProjectName>.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            "console": "internalConsole"
        },
    ],
}
Run Code Online (Sandbox Code Playgroud)

您可以在同样位于 .vscode/ 的tasks.json 文件中配置任务

{
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/<Solution Name>.sln"
            ],
            "problemMatcher": "$msCompile"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)