Developing an ASP. NET Core application using dotnet Watch
Original: Developing ASP. NET Core applications using dotnet Watch
Victor Hurdugaci
Translation: Thumbelina (Kiler)
Proofreading: Liu Yi (alexlewis), Huden (Seay)
Introduced
dotnet watch
is a tool for the development phase to use commands in the event of a change in the source file dotnet
. When the code changes, it can be used to compile, run tests, or publish operations.
In this tutorial, we will use an existing WEBAPI application that calculates the sum of two numbers and the product, demonstrating how to use it dotnet watch
. The sample application intentionally contains an error that we will fix as part of this tutorial.
Getting Started
Start downloading the sample program. The sample program contains two items WebApp
(Web application) and WebAppTests
(a unit test project that is a companion to the Web application)
In the command line console, go to the directory where you downloaded the sample program and run the following command:
1.dotnet restore
2.cd WebApp
3.dotnet run
The console output displays the following information indicating that the application is running and waiting for the request:
Copy Code
$ dotnet runproject WebApp (. Netcoreapp,version=v1. 0) 'll be compiled because inputs were modifiedcompiling WebApp for . Netcoreapp,version=v1. 0Compilation succeeded. 0 Warning (s) 0 Error (s) time elapsed :xx:02.6049991Hosting environment:productioncontent Root path: /users/user/dev/aspnet/docs/aspnet/tutorials/dotnet-watch/sample/ Webappnow listening on : http://localhost:5000application started. Press CTRL + C to shut down.
In a Web browser, navigate to the http://localhost:5000/api/math/sum?a=4&b=5
page and you will see the results 9
.
If you navigate to the http://localhost:5000/api/math/product?a=4&b=5
page, you expect to get results 20
. But it actually returned 9
.
We'll fix this problem.
Project to add
dotnet watch
1. Add references in the configuration section of the Webapp/project.json file as follows tools
Microsoft.DotNet.Watcher.Tools
:
Copy Code
"tools": { "Microsoft.DotNet.Watcher.Tools": "1.0.0-preview2-final" //手工高亮},
2, run dotnet restore
.
The console output displays the following information:
Copy Code
Log:restoring PackagesFor/users/user/dev/aspnet/docs/aspnet/tutorials/dotnet-watch/sample/webapp/project.json...log:restoring packages for tool Microsoft.DotNet.Watcher.Tools ' in /users/user/dev/aspnet/docs/aspnet/tutorials/ Dotnet-watch/sample/webapp/project.json ... log:installing Microsoft.DotNet.Watcher.Core 1.0.0-preview2-final. log:installing Microsoft.DotNet.Watcher.Tools 1.0.0- Preview2-final.
Use
dotnet watch
Run
dotnet
Command
Any dotnet
commands related to this can be dotnet watch
run in such a way: for example:
Command |
take Watch's command. |
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 |
In order to WebApp
run in watcher mode, run the command in the WebApp
directory dotnet watch run
. The console output displays the following information indicating that the dotnet watch
code file is now being monitored:
Copy Code
user$ dotnet watch Run[dotnetwatcher] info:running dotnet with The following arguments:run[dotnetwatcher] info:dotnet process id: 39746project WebApp (. Netcoreapp,version=v1.0) was previously compiled. Skipping compilation. Hosting environment:productioncontent root path: /users/user/dev/aspnet/Docs/aspnet/tutorials/dotnet-watch/sample/webappnow listening on: http://localhost : 5000Application started. Press CTRL + C to shut down.
In
dotnet watch
Mode to modify
Confirm that dotnet watch
the mode is running.
Let's fix the two-digit multiplication result that was found above error.
Open the file Webapp/controllers/mathcontroller.cs.
We intentionally introduced the error in the code.
Copy Code
public static int Product(int a, int b){ // We have an intentional bug here // + should be * return a + b;//手工高亮}
By replacing the code with the a + b
a * b
fix error.
Save the file. The console output displays the following information indicating that a dotnet watch
change to the file was detected and the application was restarted.
Copy Code
[Dotnetwatcher]Info:fileChanged/users/user/dev/aspnet/docs/aspnet/tutorials/dotnet-watch/sample/webapp/controllers/mathcontroller.cs[dotnetwatcher]Info:running dotnet with the followingArguments:run[dotnetwatcher]Info:dotnet processId:39940Project WebApp (. Netcoreapp,version=v1. 0) 'll be compiled because inputs were modifiedcompiling WebApp for . Netcoreapp,version=v1. 0Compilation succeeded. 0 Warning (s) 0 Error (s) time elapsed :xx:03.3312829Hosting environment:productioncontent Root path: /users/user/dev/aspnet/docs/aspnet/tutorials/dotnet-watch/sample/ Webappnow listening on : http://localhost:5000application started. Press CTRL + C to shut down.
Verify http://localhost:5000/api/math/product?a=4&b=5
that the link returns the correct result.
Use
dotnet watch
Run Tests
File monitoring can also run other dotnet
commands such as test
or publish
.
1. Open the WebAppTests
directory and confirm that the Project.json file is already included dotnet watch
.
2, run the dotnet watch test
command.
If you have previously MathController
fixed the error in, you will see the console output showing the following information, otherwise you will see the test failed information:
Copy Code
Webapptests user$ dotnet Watch Test[dotnetwatcher]Info:running dotnet with the followingArguments:test[dotnetwatcher]Info:dotnet processId:40193Project WebApp (. Netcoreapp,version=v1.0) was previously compiled. Skipping compilation. Project webapptests (. Netcoreapp,version=v1.0) was previously compiled. Skipping compilation.xUnit.net. NET CLI Test Runner (64-bit. NET Core OSX.10.11-x64)Discovering:webapptestsDiscovered:webapptestsStarting:webapptestsfinished:webapptests=== TEST execution SUMMARY = = = WebapptestsTotal : 2, Errors: 0, Failed: 0, skipped: 0, Time : 0.259sSUMMARY: Total : 1 targets, Passed: 1, Failed: 0.[dotnetwatcher] info:dotnet exit code: 0[dotne Twatcher] info:waiting for a file to change before restarting dotnet ...
Once all the tests are running, the monitor will instruct him to dotnet test
wait for a file change before the next reboot.
3. Open the controller Webapp/controllers/mathcontroller.cs file and modify the code. If you do not fix multiplication errors, modify them immediately. and save.
dotnet watch
A file change is detected and the test is rerun. The console output displays the following information:
Copy Code
[Dotnetwatcher]Info:fileChanged/users/user/dev/aspnet/docs/aspnet/tutorials/dotnet-watch/sample/webapp/controllers/mathcontroller.cs[dotnetwatcher]Info:running dotnet with the followingArguments:test[dotnetwatcher]Info:dotnet processId:40233Project WebApp (. Netcoreapp,version=v1.0) 'll be compiled because inputs were modifiedcompiling WebAppFor. Netcoreapp,version=v1.0Compilation succeeded.0 Warning (s)0 Error (s) time elapsed00:00:03.2127590Project webapptests (. Netcoreapp,version=v1.0) 'll be compiled because dependencies changedcompiling webapptestsFor. Netcoreapp,version=v1.0Compilation succeeded.0 Warning (s)0 Error (s) time elapsed00:00:02.1204052xunit.net. NET CLI Test Runner (64-bit. NET Core OSX.10.11-x64)Discovering:webapptestsDiscovered:webapptestsStarting:webapptestsfinished:webapptests=== TEST execution SUMMARY = = = WebapptestsTotal : 2, Errors: 0, Failed: 0, skipped: 0, Time : 0.260sSUMMARY: Total : 1 targets, Passed: 1, Failed: 0.[dotnetwatcher] info:dotnet exit code: 0[dotne Twatcher] info:waiting for a file to change before restarting dotnet ...
Back to Catalog
Developing an ASP. NET Core application using dotnet watch