如何使用Visual Studio Code为.NET Core 3.1项目设置自动监视运行?

14

我需要设置一个自动重启功能,当某些源代码文件被修改时。

我正在使用VS Code与Dotnet Core 3.1开发Web API。

当调试开始时,我可以在http:// localhost:5001 / api / entities 中看到我的REST API已发布,但如果我更改了模型或其他内容,则需要重新启动调试才能看到更改。

我尝试在终端上使用dotnet watch run启动项目并附加调试到进程,但我想知道是否可能在项目中配置所有调试以启用dotnet watch

3个回答

6

我知道这是一个旧问题,但我找到了解决方法。

我使用了Marco的解决方案,并将其添加到我的tasks.json中:

"options": {
    "cwd": "${workspaceFolder}/yourproject/"
}

因此最终解决方案是:

tasks.json

{
    "label": "watch",
    "command": "dotnet",
    "type": "process",
    "args": [
        "watch",
        "run",
        "${workspaceFolder}/yourproject/yourproject.csproj",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
    ],
    "problemMatcher": "$msCompile",
    "options": {
        "cwd": "${workspaceFolder}/yourproject/"
    }
}

launch.json

{
    "name": ".NET Core Launch (web)",
    "type": "coreclr",
    "request": "launch",
    "preLaunchTask": "watch",
    "program": "${workspaceFolder}/yourproject/bin/Debug/net5.0/yourproject.dll",
    "args": [],
    "cwd": "${workspaceFolder}/yourproject",
    "stopAtEntry": false
}

2

是的,这完全可行。

在VS Code中打开位于.vscode文件夹中的tasks.json文件。 在那里你应该可以找到一个任务数组。

最简单的方法是只需将“watch”添加到构建任务中:

"tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "watch",
                "build",
                "${workspaceFolder}/delete.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        }
 ]

由于“build”是默认任务,因此在按下F5并开始调试时,这将始终启动dotnet start build进行调试。关键是将watch添加到args数组中。

如果您想要一个专门的任务,可以在tasks.json中添加一个:

{
    "label": "watch",
    "command": "dotnet",
    "type": "process",
    "args": [
        "watch",
        "run",
        "${workspaceFolder}/delete.csproj",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
    ],
    "problemMatcher": "$msCompile"
}

在你的launch.json文件中,你可以将这个任务设置为preLaunchTask:

"configurations": [
    {
        "name": ".NET Core Launch (console)",
        "type": "coreclr",
        "request": "launch",
        "preLaunchTask": "watch", 
        "program": "${workspaceFolder}/bin/Debug/netcoreapp3.0/delete.dll",
        "args": [],
        "cwd": "${workspaceFolder}",
        "console": "internalConsole",
        "stopAtEntry": false
    }
]

我已经使用dotnet new console创建了一个小型测试项目,以便在本地尝试此功能,因此文件名为delete.dll。请根据需要进行修改。


1
很遗憾,这不起作用。 launch 阻止了 .dlls 的加载,因此每当有更改时,dotnet watch 无法更新它们并显示 The process cannot access the file 错误。 - Aleksey Shubin

0

不好意思打广告,我已经分叉并更新了dotnet自动附加vs代码扩展,并按照我的喜好进行了修改 dotnet watch enter image description here enter image description here


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