NOTE : This post was initially written in early 2017 (!!!) but has now been completely revamped in 2020 to be up to date with .NET Core 3+ Watch feature. The original article was written in pre 1.0 days and is no longer relevant. But this is still a killer feature and should be used far more often!
One of the most overlooked features of .NET Core CLI is the “dotnet watch” command. With it, it allows you to have a “live reload” of your ASP.NET Core site running without having to either run the “dotnet run” command, or worse do the “Stop the process in Visual Studio. Write your changes. Recompile and run again” routine. The latter can be very annoying when all you are trying to do is do a simple one line fix.
If you are used to watches in other languages/tooling (Especially task runners like Gulp), then you will know how much of a boost watches are to productivity.
The Basics
I highly recommend creating a simple ASP.NET Core project to run through this tutorial with. The tooling can be a little finicky with large projects so it’s easier to get up and running with something small, then take what you’ve learned onto something a little larger.
For this demo, I have a simple controller that has a get method that returns a string value.
[Route("api/home")] public class HomeController : Controller { [HttpGet] public string Get() { return "Old Value"; } }
Open a command prompt in your project directory, a terminal in VS Code, or even the Package Manager Console in Visual Studio and run the following command :
dotnet watch run
Note that if you previously have had to run the “dotnet run” command with other flags (e.g. “dotnet run -f net451” to specify the framework), this will still work using the watch command. Essentially it’s saying “Do a watch, and when something changes, do ‘this’ thing” which in our case is the run command. You should see something similar to the following :
dotnet watch run watch : Started info: Microsoft.Hosting.Lifetime[0] Now listening on: https://localhost:5001 info: Microsoft.Hosting.Lifetime[0] Now listening on: http://localhost:5000 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development
This means you are up and running. If I go to http://localhost:5000/api/home, I should see my controller return “Old Value”.
Now I head back to my controller and I change the Get action to instead return “New Value”. As soon as I hit save on this file I see the following in my console window :
watch : Exited watch : File changed: C:\Projects\WatchExample\Controllers\HomeController.cs watch : Started info: Microsoft.Hosting.Lifetime[0] Now listening on: https://localhost:5001 info: Microsoft.Hosting.Lifetime[0] Now listening on: http://localhost:5000
What we see is that the watch immediately exits, and it tells us that a file has been changed in HomeController.cs and so it starts recompiling immediately. When we browse to http://localhost:5000/api/home we see our “New Value” shown and we didn’t have to do anything else in terms of manually recompiling or running a new dotnet run command.
On larger projects, the recompile process can actually be a little slow so it’s not always an instant “change code and immediately refresh the browser” moment. Especially if you have a large dependency tree that means several projects need to be rebuilt before the site is back up and running. But it’s still going to be faster than whatever manual process you are used to.
Debugging With dotnet watch
So previously, debugging while using dotnet watch was almost a fruitless exercise. Each time your watch started and ran, a new “dotnet.exe” process would spin up. But the issue was it was impossible to find the “right” process to attach your debugger to. You might have a half dozen or so dotnet.exe processes running and you sort of had to wing it and pick the one that had been created most recent and hope for the best. Then each time you made a change, a *new* dotnet.exe would be spun up and your attached debugger was useless with you having to start the attach to debugger process all over again. Ugh!
As of .NET Core 3+, this is now much much easier.
Suppose I have my project up and running on a watch. In Visual Studio I simply go Debug -> Attach To Debugger. I then filter all processes by the actual name of my project. In my case I called my project “WatchExample”, so I just start typing that into the filter box.
I click the Attach button and I’m away! Unfortunately each time you make a change the debugger is stopped while your project recompiles, but a helpful hint is to learn the “Reattach To Process” hotkey which in default Visual Studio key bindings is “Shift + Alt + P”. This immediately attaches the debugger to the last process it was on (Which in our case, is the our project exe), and we are away debugging again!
Finally!
Aside from an error I get when I stop (Cannot detach from one or mores processes), this should save me some time. Thank you.
Seems to work now with the following:
dotnet watch –project WatchExample run
Thanks ! this issue was really bothering me.
I’ve spent weeks trying to work out how this is done. I see it in demos all the time, but they seem to have this ‘up and running’ already. I stumble across your post by accident, and now I have a much better development environment Thank you so much.
When I try to attach the debugger, my breakpoints show that the symbols are still not loading. Is there a step I’m missing? My app is a blazor wasm application.
My experience is that dotnet watch is just not stable. I waste huge amounts of time fighting VS Code to get this “feature” to work. File changes triggers only work about 70% of the time. What you then hope for is that the browser actually gets updated. Most times for me it does not. Then you get asked if you want to rebuild some of the time or all of the time. You answer and then down the rabbit hole you go. Restart the build, wait for the browser to refresh – it fails , refresh the browser, the build restart – it is a contact battle with the IDE that wastes huge amount of time.