Thursday, August 2, 2018

Using DotNet Watcher with ASP.NET Core 2.0 Application

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 on how it works.

Step 1:
Open any existing ASP.NET Core 2.0 project and open it’s project file by right clicking on project and clicking on ‘Edit XXX.csproj ‘ and add one more line as highlighted below:

    <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.2" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.2" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.1" />
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />

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

In above screenshot, I’m using 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 an information about listening port.

Step 3: Copy the listening port URL and paste it on the browser as shown below:

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

Step 4: Next is to navigate to any page in your application where you are going to modify any C# code. For me, I’m going to take About page. Initially my page is as below:

Now I’ll go to my AboutModel.cs and change message text. Quickly save the application and switch to command line window. You will notice that a red color text is displayed and application is automatically restarted. Here is the snapshot of 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 Watcher tool, refer an official link. 

Hope you enjoyed leaning this tool.

1 comment:

  1. Nice one but I think this feature is to be used only in development phase. Isn't it?