Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup an auto watch run for .NET Core 3.1 projects using Visual Studio Code?

I need to setup an auto restart when some source code file modifies.

I'm using VS Code with Dotnet Core 3.1 to develop a web api.

When debug starts I can see my REST Api published in http://localhost:5001/api/entities, but if I change a model or something else, I need to restart the debug to see the changes.

I've tried to start the project with dotnet watch run on terminal and attatch the debug to process, but I would like to know if is possible to config something in the project to start all debugs with dotnet watch enabled.

like image 564
Erick V. Avatar asked Jan 20 '20 20:01

Erick V.


2 Answers

I know this an old question, but I found a solution.

I used Marco's solution and added this to my tasks.json:

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

So the final solution is :

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
}
like image 184
ciemrnt Avatar answered Oct 03 '22 12:10

ciemrnt


Yes this is totally possible.

In VS Code, open your tasks.json, which should be located in the .vscode folder. In there you should find a tasks array.

The easiest way is to simply add "watch" to just edit the build task:

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

Since "build" is the default task, when pressing F5 and startig debugging, this will always start a dotnet start build, when debugging. The key takeaway is to add watch into the args array.

If you want to have a dedicated task for that, you can add one in the tasks.json:

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

And in your launch.json you can set this task as the 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
    }
]

I have created a small test project using dotnet new console to try this out locally, hence the delete.dll filename. Please make amendments as neccesary.

like image 44
Marco Avatar answered Oct 03 '22 12:10

Marco