在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
原文:Developing ASP.NET Core applications using dotnet watch 介绍
在本教程中,我们将使用一个现有的计算两个数字之和以及乘积的 WebApi 应用程序来演示如何使用 开始入门开始下载 示例程序。示例程序包含两个项目, 在命令行控制台中,进入下载示例程序的目录并且运行下述命令: 1、 控制台输出将显示如下信息,表明该应用程序正在运行并等待请求: 复制代码
在 Web 浏览器中,导航到 如果你导航到 我们会修复这个问题的。 项目中添加
|
命令 | 带上 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...
原文: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...
请发表评论