dotnet watch
. The sample application specifically contains an error, which we will correct during the study.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.WebApp
(web application) and WebAppTests
(unit tests for a web application).In the console, go to theWebApp
folder and execute the commands:
dotnet restore
dotnet run
$ 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.
http://localhost:5000/api/math/sum?a=4&b=5
, you will see the result of 9
.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.dotnet watch
to the project1. AddMicrosoft.DotNet.Watcher.Tools
to the .csproj file:
<ItemGroup> <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0" /> </ItemGroup>
2. Run thedotnet restore
command.
dotnet
commands using dotnet watch
dotnet
command can be executed using dotnet watch
, for example:Team | Team with watch |
---|---|
dotnet run | dotnet watch run |
dotnet run -f net451 | dotnet watch run -f net451 |
dotnet run -f net451 - --arg1 | dotnet watch run -f net451 - --arg1 |
dotnet test | dotnet watch test |
WebApp
using watcher, run the dotnet watch run
in the WebApp
folder. The console will display messages that watch
working.dotnet watch
Product
method in MathController
so that it returns the product, not the sum: public static int Product(int a, int b) { return a * b; }
dotnet watch
detected a change in the file and has restarted the application.http://localhost:5000/api/math/product?a=4&b=5
gives the correct result.dotnet watch
- Change the
Product
method inMathController
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.Source: https://habr.com/ru/post/324312/
All Articles