Using DotNet Watcher

This is a feature which can be used on the command line to watch our web application. Whenever a C# class is modified and saved, it automatically re-compiles and re-runs whatever command we pass into it. In other words, as soon as C# code is modified and saved, the watcher will see those changes and re-compiles the code without a need to close the running application.

Let’s have a look at how it works.

Step 1

Open any existing ASP.NET Core 2.0 project and open its project file by right-clicking on the project and clicking on ‘Edit XXX.csproj ‘ and add one more line as highlighted below.

  1. <ItemGroup>  
  2.     <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.2" />  
  3.     <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.2" />  
  4.     <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.1" />  
  5.     <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />  
  6. </ItemGroup>  
Step 2

Open the command prompt and navigate to the project path. Type the command as shown in below screenshot.

In the above screenshot, I’m using the run command as I want to re-run my application whenever code changes are there. If everything went well, then you will see the last line with a statement as ‘Application started. Press Ctrl+C to shut down.’ with information about listening port.
Step 3

Copy the listening port URL and paste it into the browser, as shown below.


You will notice that the site is up and running along with much more information displayed on the command line window.

Step 4

Next, navigate to any page in your application where you are going to modify any C# code. For me, I’m going to take the About page. Initially, my page looks like below.


Now, I’ll go to my AboutModel.cs and change the message text. Quickly save the application and switch to the command line window. You will notice that red text is displayed and the application is automatically restarted. Here is the snapshot of the command line.


Go to the browser and refresh the page. You will see that without closing the entire application you are able to see the updated text, as shown below.

To know more about the released versions of the Watcher tool, refer to the official link. 

Hope you enjoyed learning this tool.