AspNetCore.Docs/aspnetcore/tutorials/dotnet-watch.md

103 lines
4.3 KiB
Markdown
Raw Normal View History

2016-10-29 01:35:15 +08:00
---
title: Developing ASP.NET Core apps using dotnet watch | Microsoft Docs
2016-10-29 01:35:15 +08:00
author: rick-anderson
description: Shows how to use dotnet watch.
keywords: ASP.NET Core, using dotnet watch
2016-10-29 01:35:15 +08:00
ms.author: riande
manager: wpickett
2017-03-10 08:34:57 +08:00
ms.date: 03/09/2017
2016-10-29 01:35:15 +08:00
ms.topic: article
ms.assetid: 563ffb3f-d369-4aa5-bf0a-7300b4e7832c
2016-11-17 08:24:57 +08:00
ms.technology: aspnet
ms.prod: asp.net-core
2016-10-29 01:35:15 +08:00
uid: tutorials/dotnet-watch
---
# Developing ASP.NET Core apps using dotnet watch
2016-10-29 01:35:15 +08:00
By [Rick Anderson](https://twitter.com/RickAndMSFT) and [Victor Hurdugaci](https://twitter.com/victorhurdugaci)
2016-10-29 01:35:15 +08:00
`dotnet watch` is a tool that runs a `dotnet` command when source files change. For example, a file change can trigger compilation, tests, or deployment.
2016-10-29 01:35:15 +08:00
In this tutorial we use an existing Web API app with two endpoints: one that returns a sum and one that returns a product. The product method contains an bug that we'll fix as part of this tutorial.
2016-10-29 01:35:15 +08:00
Download the [sample app](https://github.com/aspnet/Docs/tree/master/aspnetcore/tutorials/dotnet-watch/sample). It contains two projects, `WebApp` (a web app) and `WebAppTests` (unit tests for the web app).
2016-10-29 01:35:15 +08:00
In a console, navigate to the WebApp folder and run the following commands:
2016-10-29 01:35:15 +08:00
- `dotnet restore`
- `dotnet run`
2016-10-29 01:35:15 +08:00
The console output will show messages similar to the following (indicating that the app is running and waiting for requests):
2016-10-29 01:35:15 +08:00
```console
2016-10-29 01:35:15 +08:00
$ dotnet run
Hosting environment: Production
Content root path: C:/Docs/aspnetcore/tutorials/dotnet-watch/sample/WebApp
2016-10-29 01:35:15 +08:00
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
2016-11-18 13:03:07 +08:00
```
2016-10-29 01:35:15 +08:00
In a web browser, navigate to `http://localhost:5000/api/math/sum?a=4&b=5`, you should see the result `9`.
2016-10-29 01:35:15 +08:00
Navigate to the product API (`http://localhost:5000/api/math/product?a=4&b=5`), it returns `9`, not `20` as you'd expect. We'll fix that later in the tutorial.
2016-10-29 01:35:15 +08:00
## Add `dotnet watch` to a project
2016-10-29 01:35:15 +08:00
- Add `Microsoft.DotNet.Watcher.Tools` to the *.csproj* file:
```xml
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0-msbuild3-final" />
</ItemGroup> ```
2016-10-29 01:35:15 +08:00
- Run `dotnet restore`.
2016-10-29 01:35:15 +08:00
## Running `dotnet` commands using `dotnet watch`
Any `dotnet` command can be run with `dotnet watch`, for example:
2016-10-29 01:35:15 +08:00
| Command | Command with watch |
| ---- | ----- |
| dotnet run | dotnet watch run |
2016-10-29 01:35:15 +08:00
| 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 |
Run `dotnet watch run` in the `WebApp` folder. The console output will indicate `watch` has started.
2016-10-29 01:35:15 +08:00
## Making changes with `dotnet watch`
Make sure `dotnet watch` is running.
Fix the bug in the `Product` method of the `MathController` so it returns the product and not the sum.
2016-10-29 01:35:15 +08:00
```csharp
public static int Product(int a, int b)
{
return a * b;
} ```
2016-10-29 01:35:15 +08:00
Save the file. The console output will show messages indicating that `dotnet watch` detected a file change and restarted the app.
2016-10-29 01:35:15 +08:00
Verify `http://localhost:5000/api/math/product?a=4&b=5` returns the correct result.
## Running tests using `dotnet watch`
- Change the `Product` method of the `MathController` back to returning the sum and save the file.
- In a command window, naviagate to the `WebAppTests` folder.
- Run `dotnet restore`
- Run `dotnet watch test`. You see output indicating that a test failed and that watcher is waiting for file changes:
```console
Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.
Test Run Failed.
```
- Fix the `Product` method code so it returns the product. Save the file.
`dotnet watch` detects the file change and reruns the tests. The console output will show the tests passed.
2017-03-10 08:34:57 +08:00
## dotnet-watch in GitHub
dotnet-watch is part of the GitHub [DotNetTools repository](https://github.com/aspnet/DotNetTools/tree/dev/src/Microsoft.DotNet.Watcher.Tools).
The [MSBuild section](https://github.com/aspnet/DotNetTools/blob/dev/src/Microsoft.DotNet.Watcher.Tools/README.md#msbuild) of the [dotnet-watch ReadMe](https://github.com/aspnet/DotNetTools/blob/dev/src/Microsoft.DotNet.Watcher.Tools/README.md) explains how dotnet-watch can be configured from the MSBuild project file being watched. The [dotnet-watch ReadMe](https://github.com/aspnet/DotNetTools/blob/dev/src/Microsoft.DotNet.Watcher.Tools/README.md) contains information on dotnet-watch not covered in this tutorial.