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.
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.
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:
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:
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.