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 ...
随机推荐
- 在docker中运行ASP.NET Core Web API应用程序(附AWS Windows Server 2016 widt Container实战案例)
环境准备 1.亚马逊EC2 Windows Server 2016 with Container 2.Visual Studio 2015 Enterprise(Profresianal要装Updat ...
- spring注解源码分析--how does autowired works?
1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能.我们可能会被问到,spring的注解到底是什么触发的呢?今天以spring最常使用的一个注解autowired来跟踪代码,进行d ...
- Oracle安装部署,版本升级,应用补丁快速参考
一.Oracle安装部署 1.1 单机环境 1.2 Oracle RAC环境 1.3 Oracle DataGuard环境 1.4 主机双机 1.5 客户端部署 二.Oracle版本升级 2.1 单机 ...
- python 数据类型 ----字典
字典由一对key:value 组成的 python中常用且重量级的数据类型 1. key , keys, values 字典由一对key:value 组成的 python中常用且重量级的数据类型 1. ...
- IT持续集成之质量管理
研发工具生态 质量相关工作 一次编译产出测试包与上线包 !从源头保证版本的⼀一致性!代码质量控制! 全⽅方位的⾃自动化测试体系保证! 提测冒烟效率! 全⾃自动上线流程杜绝⼈人⼯工犯错! 生产环境应⽤用 ...
- 访问者模式(visitorpattern)
/** * 访问者模式 * @author TMAC-J * 在客户端和元素之间添加一个访问者 * 当你需要添加一些和元素关系不大的需求时,可以直接放在访问者里面 * 或者是元素之间有一些公共的代码块 ...
- Lind.DDD.Aspects通过Plugins实现方法的动态拦截~Lind里的AOP
回到目录 .Net MVC之所以发展的如些之好,一个很重要原因就是它公开了一组AOP的过滤器,即使用这些过滤器可以方便的拦截controller里的action,并注入我们自己的代码逻辑,向全局的异常 ...
- BPM协同平台解决方案分享
一.需求分析 企业信息化的过程都是从单纯解决一个业务功能问题,到解决企业内部业务流程问题,再扩展到解决不同业务流程的关联互动问题, 核心是业务的集成和业务的协同,需要有一个统一的业务协同平台. 国内的 ...
- WINDOWS系统下MYSQL安装过程中的注意事项
1.首先MySQL的安装方式有两种:一种是MSI安装方式,很简单就像安装Windows软件一样.另外一种就是ZIP安装方式.这种相对而言比较麻烦.新手推荐MSI安装方式. 安装方式有以下两种: MSI ...
- Centos 7 vsftpd ldap 配置
#ldap 安裝配置 环境Centos7#安装 yum install -y openldap openldap-clients openldap-servers migrationtools pam ...