ASP.NET Core 中文文档 第二章 指南(8) 使用 dotnet watch 开发 ASP.NET Core 应用程序
本文已更新,最后更新于2017年4月27日
原文:Developing ASP.NET Core apps using dotnet watch
译文:使用 dotnet watch 开发 ASP.NET Core 应用程序
翻译:谢炀(Kiler)、刘怡(AlexLEWIS)
联系我们:
QQ Group: 436035237 (dotNet Core Studying Group)
GitHub Repo: https://github.com/dotnetcore/aspnetcore-doc-cn/
以下为老翻译存档
原文:Developing ASP.NET Core applications using dotnet watch
作者:Victor Hurdugaci
翻译:谢炀(Kiler)
校对:刘怡(AlexLEWIS)、许登洋(Seay)
介绍
dotnet watch 是一个开发阶段在源文件发生变动的情况下使用 dotnet 命令的工具。 当代码发生变动的时候可以用来执行编译,运行测试,或者发布操作。
在本教程中,我们将使用一个现有的计算两个数字之和以及乘积的 WebApi 应用程序来演示如何使用 dotnet watch 。示例应用程序故意包含一个错误,作为本教程的一部分我们会修复它。
开始入门
开始下载 示例程序。示例程序包含两个项目, WebApp (Web 应用程序)以及 WebAppTests (Web 应用程序配套的单元测试项目)
在命令行控制台中,进入下载示例程序的目录并且运行下述命令:
1、dotnet restore
2、cd WebApp
3、dotnet run
控制台输出将显示如下信息,表明该应用程序正在运行并等待请求:
$ dotnet run
Project WebApp (.NETCoreApp,Version=v1.0) will be compiled because inputs were modified
Compiling WebApp for .NETCoreApp,Version=v1.0
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:02.6049991
Hosting environment: Production
Content root path: /Users/user/dev/aspnet/Docs/aspnet/tutorials/dotnet-watch/sample/WebApp
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
在 Web 浏览器中,导航到 http://localhost:5000/api/math/sum?a=4&b=5 页面你会看到结果 9 。
如果你导航到 http://localhost:5000/api/math/product?a=4&b=5 页面,你期望得到结果 20。但是实际上还是返回了 9 。
我们会修复这个问题的。
项目中添加 dotnet watch
1、按照下面例子的方式在 WebApp/project.json 文件的 tools 配置节中添加 Microsoft.DotNet.Watcher.Tools 引用:
"tools": {
"Microsoft.DotNet.Watcher.Tools": "1.0.0-preview2-final" //手工高亮
},
2、运行 dotnet restore。
控制台输出将显示如下信息:
log : Restoring packages for /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.
使用 dotnet watch 运行 dotnet 命令
任何与 dotnet 有关的命令都可以以 dotnet watch 这样的方式运行:例如:
| 命令 | 带上 watch 的命令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 |
为了让 WebApp 在 watcher 模式下运行,在 WebApp 目录里面运行 dotnet watch run 命令。 控制台输出将显示如下信息,表明 dotnet watch 现在正在监控代码文件:
user$ dotnet watch run
[DotNetWatcher] info: Running dotnet with the following arguments: run
[DotNetWatcher] info: dotnet process id: 39746
Project WebApp (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Hosting environment: Production
Content root path: /Users/user/dev/aspnet/Docs/aspnet/tutorials/dotnet-watch/sample/WebApp
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
在 dotnet watch 模式进行修改
确认 dotnet watch 模式运行中。
让我们来修复上面发现的那个两个数相乘结果错误。
打开文件 WebApp/Controllers/MathController.cs。
我们故意在代码中引入了错误。
public static int Product(int a, int b)
{
// We have an intentional bug here
// + should be *
return a + b;//手工高亮
}
通过把代码 a + b 替换为 a * b 修复错误。
保存文件。 控制台输出将显示如下信息,表明 dotnet watch 检测到文件的改变并重启了应用程序。
[DotNetWatcher] info: File changed: /Users/user/dev/aspnet/Docs/aspnet/tutorials/dotnet-watch/sample/WebApp/Controllers/MathController.cs
[DotNetWatcher] info: Running dotnet with the following arguments: run
[DotNetWatcher] info: dotnet process id: 39940
Project WebApp (.NETCoreApp,Version=v1.0) will be compiled because inputs were modified
Compiling WebApp for .NETCoreApp,Version=v1.0
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:03.3312829
Hosting environment: Production
Content root path: /Users/user/dev/aspnet/Docs/aspnet/tutorials/dotnet-watch/sample/WebApp
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
验证 http://localhost:5000/api/math/product?a=4&b=5 链接返回正确的结果。
使用 dotnet watch 运行测试
文件监控也能运行其他 dotnet 命令例如 test 或者 publish。
1、打开 WebAppTests 目录,确认 project.json 文件中已经包含了 dotnet watch。
2、运行 dotnet watch test 命令。
如果你之前在 MathController 中修复了错误你会看到控制台输出显示如下信息,否则你会看到测试失败的信息:
WebAppTests user$ dotnet watch test
[DotNetWatcher] info: Running dotnet with the following arguments: test
[DotNetWatcher] info: dotnet process id: 40193
Project 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: WebAppTests
Discovered: WebAppTests
Starting: WebAppTests
Finished: WebAppTests
=== TEST EXECUTION SUMMARY ===
WebAppTests Total: 2, Errors: 0, Failed: 0, Skipped: 0, Time: 0.259s
SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.
[DotNetWatcher] info: dotnet exit code: 0
[DotNetWatcher] info: Waiting for a file to change before restarting dotnet...
一旦所有的测试运行起来了,监控器会指示他在下一次重新启动 dotnet test 前会等待一个文件的变更。
3、打开控制器 WebApp/Controllers/MathController.cs 文件并且修改代码。如果你没有修复乘法错误,马上修改。并保存。
dotnet watch 将会检测到文件变更并且重新运行测试。 控制台输出将显示如下信息:
[DotNetWatcher] info: File changed: /Users/user/dev/aspnet/Docs/aspnet/tutorials/dotnet-watch/sample/WebApp/Controllers/MathController.cs
[DotNetWatcher] info: Running dotnet with the following arguments: test
[DotNetWatcher] info: dotnet process id: 40233
Project WebApp (.NETCoreApp,Version=v1.0) will be compiled because inputs were modified
Compiling WebApp for .NETCoreApp,Version=v1.0
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:03.2127590
Project WebAppTests (.NETCoreApp,Version=v1.0) will be compiled because dependencies changed
Compiling WebAppTests for .NETCoreApp,Version=v1.0
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:02.1204052
xUnit.net .NET CLI test runner (64-bit .NET Core osx.10.11-x64)
Discovering: WebAppTests
Discovered: WebAppTests
Starting: WebAppTests
Finished: WebAppTests
=== TEST EXECUTION SUMMARY ===
WebAppTests Total: 2, Errors: 0, Failed: 0, Skipped: 0, Time: 0.260s
SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.
[DotNetWatcher] info: dotnet exit code: 0
[DotNetWatcher] info: Waiting for a file to change before restarting dotnet...
ASP.NET Core 中文文档 第二章 指南(8) 使用 dotnet watch 开发 ASP.NET Core 应用程序的更多相关文章
- ASP.NET Core 中文文档 第二章 指南(4.6)Controller 方法与视图
原文:Controller methods and views 作者:Rick Anderson 翻译:谢炀(Kiler) 校对:孟帅洋(书缘) .张仁建(第二年.夏) .许登洋(Seay) .姚阿勇 ...
- ASP.NET Core 中文文档 第二章 指南(4.4)添加 Model
原文:Adding a model 作者:Rick Anderson 翻译:娄宇(Lyrics) 校对:许登洋(Seay).孟帅洋(书缘).姚阿勇(Mr.Yao).夏申斌 在这一节里,你将添加一些类来 ...
- ASP.NET Core 中文文档 第二章 指南(4.9)添加验证
原文:Adding Validation 作者:Rick Anderson 翻译:谢炀(Kiler) 校对:孟帅洋(书缘).娄宇(Lyrics).许登洋(Seay) 在本章节中你将为 Movie 模型 ...
- ASP.NET Core 中文文档 第二章 指南(1)用 Visual Studio Code 在 macOS 上创建首个 ASP.NET Core 应用程序
原文:Your First ASP.NET Core Application on a Mac Using Visual Studio Code 作者:Daniel Roth.Steve Smith ...
- ASP.NET Core 中文文档 第二章 指南 (09) 使用 Swagger 生成 ASP.NET Web API 在线帮助测试文档
原文:ASP.NET Web API Help Pages using Swagger 作者:Shayne Boyer 翻译:谢炀(kiler) 翻译:许登洋(Seay) 对于开发人员来说,构建一个消 ...
- ASP.NET Core 中文文档 第二章 指南(2)用 Visual Studio 和 ASP.NET Core MVC 创建首个 Web API
原文:Building Your First Web API with ASP.NET Core MVC and Visual Studio 作者:Mike Wasson 和 Rick Anderso ...
- ASP.NET Core 中文文档 第二章 指南(3)用 Visual Studio 发布一个 Azure 云 Web 应用程序
原文:Getting Started 作者:Rick Anderson 翻译:谢炀(Kiler) 校对:孟帅洋(书缘).刘怡(AlexLEWIS).何镇汐 设置开发环境 安装最新版本的 Azure S ...
- ASP.NET Core 中文文档 第二章 指南(4.1)ASP.NET Core MVC 与 Visual Studio 入门
原文:Getting started with ASP.NET Core MVC and Visual Studio 作者:Rick Anderson 翻译:娄宇(Lyrics) 校对:刘怡(Alex ...
- ASP.NET Core 中文文档 第二章 指南(4.5)使用 SQL Server LocalDB
原文:Working with SQL Server LocalDB 作者:Rick Anderson 翻译: 魏美娟(初见) 校对: 孟帅洋(书缘).张硕(Apple).许登洋(Seay) Appl ...
随机推荐
- ASP.NET Aries 入门开发教程9:业务表单的开发
前言: 经过前面那么多篇的列表的介绍,终于到了大伙期待的表单开发了. 也是本系列的最后一篇文章了! 1:表单页面的权限设置与继承 对于表单页面,权限的设置有两种: 1:你可以选择添加菜单(设置为不显示 ...
- 23种设计模式--责任链模式-Chain of Responsibility Pattern
一.责任链模式的介绍 责任链模式用简单点的话来说,将责任一步一步传下去,这就是责任,想到这个我们可以相当击鼓传花,这个是为了方便记忆,另外就是我们在项目中经常用到的审批流程等这一类的场景时我们就可以考 ...
- 算法与数据结构(十七) 基数排序(Swift 3.0版)
前面几篇博客我们已经陆陆续续的为大家介绍了7种排序方式,今天博客的主题依然与排序算法相关.今天这篇博客就来聊聊基数排序,基数排序算法是不稳定的排序算法,在排序数字较小的情况下,基数排序算法的效率还是比 ...
- 步入angularjs directive(指令)--准备工作熟悉hasOwnProperty
在讲解directive之前,先做一下准备工作,为何要这样呢? 因为我们不是简单的说说directive怎么用,还要知道为什么这么用!(今天我们先磨磨刀!). 首先我们讲讲js 基础的知识--hasO ...
- 脑洞大开之采用HTML5+SignalR2.0(.Net)实现原生Web视频
目录 对SignalR不了解的人可以直接移步下面的目录 SignalR系列目录 前言 - -,我又来了,今天废话不多说,我们直接来实现Web视频聊天. 采用的技术如下: HTML5 WebRTC Si ...
- Performance Monitor4:监控SQL Server的IO性能
SQL Server的IO性能受到物理Disk的IO延迟和SQL Server内部执行的IO操作的影响.在监控Disk性能时,最主要的度量值(metric)是IO延迟,IO延迟是指从Applicati ...
- Linux主机上使用交叉编译移植u-boot到树莓派
0环境 Linux主机OS:Ubuntu14.04 64位,运行在wmware workstation 10虚拟机 树莓派版本:raspberry pi 2 B型. 树莓派OS: Debian Jes ...
- 从源码浅析MVC的MvcRouteHandler、MvcHandler和MvcHttpHandler
熟悉WebForm开发的朋友一定都知道,Page类必须实现一个接口,就是IHttpHandler.HttpHandler是一个HTTP请求的真正处理中心,在HttpHandler容器中,ASP.NET ...
- ORACLE从共享池删除指定SQL的执行计划
Oracle 11g在DBMS_SHARED_POOL包中引入了一个名为PURGE的新存储过程,用于从对象库缓存中刷新特定对象,例如游标,包,序列,触发器等.也就是说可以删除.清理特定SQL的执行计划 ...
- (转载)linux下各个文件夹的作用
linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基 ...