2016-10-29 01:35:15 +08:00
---
2018-06-01 04:09:51 +08:00
title: Develop ASP.NET Core apps using a file watcher
2016-10-29 01:35:15 +08:00
author: rick-anderson
2018-06-01 04:09:51 +08:00
description: This tutorial demonstrates how to install and use the .NET Core CLI's file watcher (dotnet watch) tool in an ASP.NET Core app.
2018-01-29 23:21:31 +08:00
ms.author: riande
2018-06-01 04:09:51 +08:00
ms.date: 05/31/2018
2016-10-29 01:35:15 +08:00
uid: tutorials/dotnet-watch
---
2018-06-01 04:09:51 +08:00
# Develop ASP.NET Core apps using a file watcher
2016-10-29 01:35:15 +08:00
2017-02-14 03:59:08 +08:00
By [Rick Anderson ](https://twitter.com/RickAndMSFT ) and [Victor Hurdugaci ](https://twitter.com/victorhurdugaci )
2016-10-29 01:35:15 +08:00
2017-10-06 03:39:38 +08:00
`dotnet watch` is a tool that runs a [.NET Core CLI ](/dotnet/core/tools ) command when source files change. For example, a file change can trigger compilation, test execution, or deployment.
2016-10-29 01:35:15 +08:00
2018-06-01 04:09:51 +08:00
This tutorial uses an existing web API with two endpoints: one that returns a sum and one that returns a product. The product method has a bug, which is fixed in this tutorial.
2016-10-29 01:35:15 +08:00
2018-06-01 04:09:51 +08:00
Download the [sample app ](https://github.com/aspnet/Docs/tree/master/aspnetcore/tutorials/dotnet-watch/sample ). It consists of two projects: *WebApp* (an ASP.NET Core web API) and *WebAppTests* (unit tests for the web API).
2016-10-29 01:35:15 +08:00
2018-06-01 04:09:51 +08:00
In a command shell, navigate to the *WebApp* folder. Run the following command:
2016-10-29 01:35:15 +08:00
2017-10-06 02:45:50 +08:00
```console
dotnet run
```
2016-10-29 01:35:15 +08:00
2017-10-06 02:45:50 +08:00
The console output shows messages similar to the following (indicating that the app is running and awaiting requests):
2016-10-29 01:35:15 +08:00
2017-02-14 03:59:08 +08:00
```console
2016-10-29 01:35:15 +08:00
$ dotnet run
2017-10-06 02:45:50 +08:00
Hosting environment: Development
2017-02-14 03:59:08 +08:00
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
2017-10-06 04:22:07 +08:00
In a web browser, navigate to `http://localhost:<port number>/api/math/sum?a=4&b=5` . You should see the result of `9` .
2016-10-29 01:35:15 +08:00
2018-06-01 04:09:51 +08:00
Navigate to the product API (`http://localhost:< port number > /api/math/product?a=4& b=5`). It returns `9` , not `20` as you'd expect. That problem is fixed later in the tutorial.
::: moniker range="< = aspnetcore-2.0"
2016-10-29 01:35:15 +08:00
2017-02-14 03:59:08 +08:00
## Add `dotnet watch` to a project
2016-10-29 01:35:15 +08:00
2018-06-01 04:09:51 +08:00
The `dotnet watch` file watcher tool is included with version 2.1.300 of the .NET Core SDK. The following steps are required when using an earlier version of the .NET Core SDK.
2017-10-06 03:39:38 +08:00
1. Add a `Microsoft.DotNet.Watcher.Tools` package reference to the *.csproj* file:
2017-10-06 02:45:50 +08:00
2017-10-06 03:39:38 +08:00
```xml
< ItemGroup >
< DotNetCliToolReference Include = "Microsoft.DotNet.Watcher.Tools" Version = "2.0.0" / >
2018-06-01 04:09:51 +08:00
< / ItemGroup >
2017-10-06 03:39:38 +08:00
```
2016-10-29 01:35:15 +08:00
2017-10-06 04:22:07 +08:00
1. Install the `Microsoft.DotNet.Watcher.Tools` package by running the following command:
2018-06-01 04:09:51 +08:00
2017-10-06 04:22:07 +08:00
```console
dotnet restore
```
2016-10-29 01:35:15 +08:00
2018-06-01 04:09:51 +08:00
::: moniker-end
## Run .NET Core CLI commands using `dotnet watch`
2016-10-29 01:35:15 +08:00
2017-10-06 03:39:38 +08:00
Any [.NET Core CLI command ](/dotnet/core/tools#cli-commands ) can be run with `dotnet watch` . For example:
2016-10-29 01:35:15 +08:00
| Command | Command with watch |
| ---- | ----- |
2017-02-14 03:59:08 +08:00
| dotnet run | dotnet watch run |
2017-10-06 03:39:38 +08:00
| dotnet run -f netcoreapp2.0 | dotnet watch run -f netcoreapp2.0 |
| dotnet run -f netcoreapp2.0 -- --arg1 | dotnet watch run -f netcoreapp2.0 -- --arg1 |
2016-10-29 01:35:15 +08:00
| dotnet test | dotnet watch test |
2017-10-06 02:45:50 +08:00
Run `dotnet watch run` in the *WebApp* folder. The console output indicates `watch` has started.
2016-10-29 01:35:15 +08:00
2018-06-01 04:09:51 +08:00
## Make changes with `dotnet watch`
2016-10-29 01:35:15 +08:00
Make sure `dotnet watch` is running.
2017-10-06 03:06:30 +08:00
Fix the bug in the `Product` method of *MathController.cs* so it returns the product and not the sum:
2016-10-29 01:35:15 +08:00
2017-02-14 03:59:08 +08:00
```csharp
public static int Product(int a, int b)
{
return a * b;
2018-06-01 04:09:51 +08:00
}
2017-04-07 05:38:57 +08:00
```
2016-10-29 01:35:15 +08:00
2017-10-06 03:06:30 +08:00
Save the file. The console output indicates that `dotnet watch` detected a file change and restarted the app.
2016-10-29 01:35:15 +08:00
2017-10-06 02:45:50 +08:00
Verify `http://localhost:<port number>/api/math/product?a=4&b=5` returns the correct result.
2016-10-29 01:35:15 +08:00
2018-06-01 04:09:51 +08:00
## Run tests using `dotnet watch`
2016-10-29 01:35:15 +08:00
2018-06-01 04:09:51 +08:00
1. Change the `Product` method of *MathController.cs* back to returning the sum. Save the file.
2017-10-06 03:39:38 +08:00
1. In a command shell, navigate to the *WebAppTests* folder.
2018-02-25 00:10:22 +08:00
1. Run [dotnet restore ](/dotnet/core/tools/dotnet-restore ).
2018-06-01 04:09:51 +08:00
1. Run `dotnet watch test` . Its output indicates that a test failed and that the watcher is awaiting file changes:
2017-02-14 03:59:08 +08:00
2017-10-06 03:39:38 +08:00
```console
Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.
Test Run Failed.
```
2017-10-06 02:45:50 +08:00
2017-10-06 03:39:38 +08:00
1. Fix the `Product` method code so it returns the product. Save the file.
2017-02-14 03:59:08 +08:00
2017-10-06 02:45:50 +08:00
`dotnet watch` detects the file change and reruns the tests. The console output indicates the tests passed.
2017-03-10 08:34:57 +08:00
2018-06-12 05:19:52 +08:00
## Customize files list to watch
2017-03-10 08:34:57 +08:00
2018-06-12 05:19:52 +08:00
By default, `dotnet-watch` tracks all files matching the following glob patterns:
2017-03-10 08:34:57 +08:00
2018-06-12 05:19:52 +08:00
* `**/*.cs`
* `*.csproj`
* `**/*.resx`
More items can be added to the watch list by editing the *.csproj* file. Items can be specified individually or by using glob patterns.
```xml
< ItemGroup >
<!-- extends watching group to include *.js files -->
< Watch Include = "** \*.js" Exclude = "node_modules \** \*;** \*.js.map;obj \** \*;bin \** \*" />
< / ItemGroup >
```
## Opt-out of files to be watched
`dotnet-watch` can be configured to ignore its default settings. To ignore specific files, add the `Watch="false"` attribute to an item's definition in the *.csproj* file:
```xml
< ItemGroup >
<!-- exclude Generated.cs from dotnet - watch -->
< Compile Include = "Generated.cs" Watch = "false" / >
<!-- exclude Strings.resx from dotnet - watch -->
< EmbeddedResource Include = "Strings.resx" Watch = "false" / >
<!-- exclude changes in this referenced project -->
< ProjectReference Include = ".. \ClassLibrary1 \ClassLibrary1.csproj" Watch = "false" />
< / ItemGroup >
```
## Custom watch projects
`dotnet-watch` isn't restricted to C# projects. Custom watch projects can be created to handle different scenarios. Consider the following project layout:
* **test/**
* *UnitTests/UnitTests.csproj*
* *IntegrationTests/IntegrationTests.csproj*
If the goal is to watch both projects, create a custom project file configured to watch both projects:
```xml
< Project >
< ItemGroup >
< TestProjects Include = "** \*.csproj" />
< Watch Include = "** \*.cs" />
< / ItemGroup >
< Target Name = "Test" >
< MSBuild Targets = "VSTest" Projects = "@(TestProjects)" / >
< / Target >
< Import Project = "$(MSBuildExtensionsPath) \Microsoft.Common.targets" />
< / Project >
```
To start file watching on both projects, change to the *test* folder. Execute the following command:
```console
dotnet watch msbuild /t:Test
```
VSTest executes when any file changes in either test project.
## `dotnet-watch` in GitHub
2018-07-14 05:33:06 +08:00
`dotnet-watch` is part of the GitHub [DotNetTools repository ](https://github.com/aspnet/DotNetTools/tree/master/src/dotnet-watch ).