📜 ⬆️ ⬇️

ASP.NET Core: Developing ASP.NET Core Applications with dotnet watch

In this tutorial, we will use the existing WebApi application (it calculates the sum and product of two numbers) to demonstrate the use dotnet watch . The sample application specifically contains an error, which we will correct during the study.



The second cycle of articles on ASP.NET Core


1. Creating server services for mobile applications .
2. Develop ASP.NET Core applications using dotnet watch .
3. Creating ASP.NET web API reference pages using Swagger .
4. Open web interface for .NET (OWIN).
5. Choosing the right .NET development environment on the server.

Introduction


dotnet watch is a developer tool that executes the dotnet command when source files change. With it, you can compile, test or publish changes to the code.
')

Beginning of work


First download the sample application. It contains two projects, WebApp (web application) and WebAppTests (unit tests for a web application).
In the console, go to the WebApp folder and execute the commands:
  • dotnet restore
  • dotnet run

The console will display messages (example below) that indicate that the application is running and waiting for requests.

 $ dotnet run Hosting environment: Production Content root path: C:/Docs/aspnetcore/tutorials/dotnet-watch/sample/WebApp Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down. 

In your browser, go to http://localhost:5000/api/math/sum?a=4&b=5 , you will see the result of 9 .

If you go to http://localhost:5000/api/math/product?a=4&b=5 , again get 9 instead of the expected 4 * 5 = 20 . We will fix this below.

Adding dotnet watch to the project


1. Add Microsoft.DotNet.Watcher.Tools to the .csproj file:

 <ItemGroup> <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0" /> </ItemGroup> 

2. Run the dotnet restore command.

Running dotnet commands using dotnet watch


Any dotnet command can be executed using dotnet watch , for example:
TeamTeam with watch
dotnet rundotnet watch run
dotnet run -f net451dotnet watch run -f net451
dotnet run -f net451 - --arg1dotnet watch run -f net451 - --arg1
dotnet testdotnet watch test

To perform a WebApp using watcher, run the dotnet watch run in the WebApp folder. The console will display messages that watch working.

Changes with dotnet watch


Make sure the dotnet watch works.

Let's correct the error in the Product method in MathController so that it returns the product, not the sum:

 public static int Product(int a, int b) { return a * b; } 

Save the file. The console displays messages that indicate that dotnet watch detected a change in the file and has restarted the application.

Make sure that http://localhost:5000/api/math/product?a=4&b=5 gives the correct result.

Test execution with dotnet watch


  • Change the Product method in MathController to return the amount and save the file.
  • On the command line, go to WebAppTests .
  • Run dotnet restore .
  • Run the dotnet watch test . You will see a message that the test failed and the watcher is waiting for a change in the file:

     Total tests: 2. Passed: 1. Failed: 1. Skipped: 0. Test Run Failed. 

  • Correct the Product method so that it returns the work.

dotnet watch detect the change in the file and restart the tests. The console will display a message that the test was successful.

dotnet-watch on dotnet-watch


dotnet-watch is part of the DotNetTools repository. Everything that you did not find in this guide can be found there.

UPD: Thank you for the correction of Ilya .

Source: https://habr.com/ru/post/324312/


All Articles