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:
- Execute the โdotnet runโ command in the root folder of application.
- 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:
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.
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:
- Dotnet test NUnit: It is used for NUnit testing framework.
- Dotnet test XUnit: It is used for the XUnit testing framework.
- Dotnet test mstest: It is used for the mstest framework.
The runners mentioned above are in the pre-release stage. For their use in Nuget, make sure that the checkbox of โInclude Prereleaseโ is selected.
Creating a sample Application:
- 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.
- Create another Class Library project as a test project. Make all the required changes in project.json file.
- 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.
- 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.
- 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 in ASP.Net. 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.