Home / Articles / Using Dotnet Watch Tool in Asp.net

Using Dotnet Watch Tool in Asp.net

One of the exciting features of Asp.Net 5 is to track all changes in code without any need to recompile the entire project. It makes the development process much faster and simpler. If the .Net application is run with IIS express (using Ctlr+F5), then it is possible to make code changes in Visual Studio, save them and see the updated changes by refreshing the browser. However, most of the programmers use only Kestrel for directly launching their apps. The same mode is achieved in two ways:

  1. Execute the ‘dotnet run’ command in the root folder of application.
  2. Switch the same directly from Visual Studio.

Dotnet watch tool works like a Dotnet file watcher that performs application restart whenever any code change is detected in the source code. It is also used to run test, publish and compile applications whenever there is some code change. It works like nodemon in nodejs. In case where IIS Express is used, the restart is already done for the user. This watch tool is useful in cases where the app needs to be run in console. The most important feature of this tool is that it will show the logs flashing in console. It comes by default in the Update 3 version of Visual Studio 2015. The screenshot is shown below:

Set up the Watch Tool

Set up the Watch Tool:

It is very simple to set up the Dotnet watch tool. Install the following package in tools section of project.json file.

Microsoft.Dotnet.Watcher.Tools

The package needs to be restored manually since the tooling feature will not take care of this part. Now open the PowerShell and go to the project folder. Run the command ‘Dotnet Watch Run’ and you are done. It is somewhat lame to use a command line. Visual Studio can perform the job in better way.

Launch Application:

For launching the application in Visual studio, launchSettings.Json file is needed. To launch the application with Dotnet watch tool, additional settings are added in this file.

Integrating Dotnet Watch Tool with Visual Studio Code in Asp.Net Application:

To use the Dotnet Watch Tool in Asp.Net application, firstly install the tool. Now use the dotnet watch run command that will execute the application and look for changes made in directory. In case any change is found, it will again run the application and build the entire project. The dotnet commands are not supported by Visual Studio directly. As an alternative, the task runners of JavaScript such as Gulp or Grunt could be used. The tasks are supported by the Visual Studio code. Once the project is opened in the Visual Studio code, it will prompt for installation of the assets needed to debug and build the project.

Integrating Dotnet Watch Tool with Visual Studio Code in Asp.Net Application

After confirming the installation, two files will be created by the Visual Studio code in the .vscode folder. Open tasks.json file. In this file, a build task will be created by default. Add another task similar to this in the file and set its IsWatchingCommand to true as shown below:

{

“taskName”: “watch”,

“args”: [

“run”

],

“isWatching”: true

}

Now execute the watch task with the help of Command Palette from the Visual Studio code and click on the option to run task. Select the available watch command. It will in turn run the command ‘dotnet watch run’. To end the command execution, click on the option ‘terminate running task’ from the Command Palette.

Using Dotnet Watch Tool in NUnit tests:

Now it is possible to write unit tests with NUnit in .Net Core and watch the same for any change. Once a change is found, the test suite is re-run on fly. The same feature is also available with NChrunch, however it is free in .Net core.

In order to run a unit test in .Net Core, a test runner is required. Following are few dotnet test runners:

  1. Dotnet test NUnit: It is used for NUnit testing framework.
  2. Dotnet test XUnit: It is used for the XUnit testing framework.
  3. Dotnet test mstest: It is used for the mstest framework.

The runners mentioned above are in the prerelease stage. For their use in Nuget, make sure that the checkbox of ‘Include Prerelease’ is selected.

Creating a sample Application:

  1. Create a new project of type class library. Click on file, and then select Add, then New Project. Click on .Net core and select the class Library project from wizard.
  2. Create another Class Library project as a test project. Make all the required changes in project.json file.
  3. Next, download the package for NUnit and the corresponding runner (dotnet test NUnit) for the .Net core. Make sure that the framework node is targeted to netcoreapp1.0 to make compatibility between the runner and the project; otherwise there will be errors while performing the build.
  4. Install the dotnet watch tool in the project from the section of tools. Download ‘Microsoft.Dotnet.Watcher.Tools’ package. It will in turn install all the CLI tools required to execute the command of dotnet watch.
  5. Lastly, the test runner should be set to NUnit. In case XUnit framework is used, then set the test runner to xunit and to mstest for the MSTest framework.

To run the dotnet watch regularly, open the folder of test project along with a command window. Type dotnet watch test for the required directory.

The test suite will run as soon as a change is detected in the test or in the code.

Is there anything you didn’t get? You can ask asp.net development India team about Dotnet Watch tool and its use in MVC via making comments below. You can even share your experience while using this tool with other readers here.

About Aaron Jacobson

Aaron Jacobson is a web developer working at Technoligent. You can contact her in order to hire .Net developers to avail the highly asp.net development India and other web service. He has several years of experience in the field of web development.

Check Also

10 Modern and Interactive Web Design Examples - Thumb

10 Modern and Interactive Web Design Examples

Web design is the best medium to communicate with users from all over the world. …

Leave a Reply

Your email address will not be published. Required fields are marked *