The Dotnet Watch Tool

Microsoft .NET

The dotnet watch tool is a file watcher for .NET that restarts the application when changes in the source code are detected. If you are using IIS Express then, it actually does this restart for you already. The dotnet watch tool is only really useful if you like to run your app in the console. I personally like to do this over using IIS Express because I can see all my logs flashing by in the console like the movies which is occasionally useful if you get an exception.

Dotnet Watch Run Console

Warning

In both cases you have to be careful to start the application by clicking Debug -> Start Without Debugging or hitting the CTRL+F5 keyboard shortcut.

project.json

Setting up the dotnet watch tool is as easy as installing the Microsoft.DotNet.Watcher.Tools NuGet package into the tools section of your project.json file like so (You may need to manually restore packages as there is a bug in the tooling which doesn't restore packages if you only change the tools section):

{
  //...

  "tools": {
    "Microsoft.DotNet.Watcher.Tools": "1.0.0-preview2-final"
    //...
  },

  //...
}

Now using powershell, you can navigate to your project folder and run the dotnet watch run command and your set. But using the command line is a bit lame if you are using Visual Studio, we can do one better.

launchSettings.json

The launchSettings.json file is used by Visual Studio to launch your application and controls what happens when you hit F5. It turns out you can add additional launch settings here to launch the application using the dotnet watch tool. You can do so by adding a new launch configuration as I've done at the bottom of this file:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:8080/",
      "sslPort": 44300
    }
  },
  "profiles": {
    // Run the app using IIS Express. Use CTRL+F5 or Debug -> Start Without Debugging to edit code and refresh the browser 
    // to see your changes while the app is running.
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "https://localhost:44300/",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    // Run the app in console mode using 'dotnet run'.
    "dotnet run": {
      "commandName": "Project",
      "commandLineArgs": "--server.urls http://*:8080",
      "launchBrowser": true,
      "launchUrl": "http://localhost:8080/",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    // Use CTRL+F5 or Debug -> Start Without Debugging to use this launch profile. Launches the app using 'dotnet watch', 
    // which allows you to edit code and refresh the browser to see your changes while the app is running.
    "dotnet watch": {
      "executablePath": "C:\\Program Files\\dotnet\\dotnet.exe",
      "commandLineArgs": "watch run --server.urls http://*:8080",
      "launchBrowser": true,
      "launchUrl": "http://localhost:8080/",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Notice that I renamed the second launch profile (which already exists in the default template) to dotnet run because that's actually the command it's running and makes more sense.

The dotnet watch launch profile is running dotnet watch run but it's also passing in the server.urls argument which lets us override the port number. Now we can see the new launch profile in the Visual Studio toolbar like so:

Dotnet Watch in the Visual Studio Toolbar

.NET Boxed

If you read my blog posts, you'll be seeing a trend by now. I built the above feature into the .NET Boxed project templates by default so you can create a new project with this feature built-in, right out of the box. Happy coding!

Web Mentions

What's this?

0 Replies

GitHub Comments

RehanSaeed

Duncan Duncan commented on 2016-09-12 12:07:36

Useful article!
What tool do you use for your blog as it looks clean and efficient?!

RehanSaeed

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2016-09-13 08:38:15

Useful article!
What tool do you use for your blog as it looks clean and efficient?!

It's Nami WordPress (So I can run on Azure with SQL Server) with a Themify theme.

RehanSaeed

Andrew Lock Andrew Lock commented on 2016-09-15 09:07:26

Great post - I've generally been using the watcher tool from the command line for all my core work, hooking into the launcher tool makes so much sense!

RehanSaeed

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2016-09-15 11:14:37

Great post - I've generally been using the watcher tool from the command line for all my core work, hooking into the launcher tool makes so much sense!

Watching the command line just helps so much with debugging sometimes.

RehanSaeed

Danial Danial commented on 2016-11-21 10:06:54

Thanks for the great article! thumbs up

RehanSaeed

saillesh Pawar saillesh Pawar commented on 2017-02-15 19:57:15

Excellent post was thinking how useful it would be if I can do the same visual studio glad you have already done, learned how Visual Studio to launch our application and controls what
happens when we hit f5.

RehanSaeed

saillesh Pawar saillesh Pawar commented on 2017-02-15 20:21:28

Hi Rehan while executing the dotnet watch launch profile i am getting below error:

watch : Could not find a MSBuild project file in 'project name'. Specify which project to use with the --project option.

RehanSaeed

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2017-02-16 10:14:00

Hi Rehan while executing the dotnet watch launch profile i am getting below error:

watch : Could not find a MSBuild project file in 'project name'. Specify which project to use with the --project option.

You need to hit Ctrl+F5 to use dotnet watch run.

RehanSaeed

Jasonlhy Jasonlhy commented on 2017-02-28 06:55:02

Cool, thanks a lot. But it seems that the port number cannot be specified now

RehanSaeed

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2017-02-28 11:05:47

Cool, thanks a lot. But it seems that the port number cannot be specified now

What do you mean? Seems to work fine for me. Are you sure you have not hard coded the port number in Program.cs?