无法在 dotnetcore 2.1.105 上安装 dotnet watcher 工具

Can't install dotnet watcher tools on dotnetcore 2.1.105

我从 here 在 ubuntu 上安装了 .NET Core 2.1.105 并使用 dotnet new api-test

创建了示例 Web api 项目

然后我尝试添加 dotnet watcher 包

dotnet add package Microsoft.DotNet.Watcher.Tools

当我发出 dotnet restore 时,出现以下错误:

$ dotnet restore
  Restoring packages for ~/devel/apps/dotnet/api_test/api_test.csproj...
  Restore completed in 101.8 ms for ~/devel/apps/dotnet/api_test/api_test.csproj.
~/devel/apps/dotnet/api_test/api_test.csproj : error NU1605: Detected package downgrade: Microsoft.NETCore.App from 2.0.6 to 2.0.0. Reference the package directly from the project to select a different version. 
~/devel/apps/dotnet/api_test/api_test.csproj : error NU1605:  api_test -> Microsoft.DotNet.Watcher.Tools 2.0.1 -> Microsoft.NETCore.App (>= 2.0.6) 
~/devel/apps/dotnet/api_test/api_test.csproj : error NU1605:  api_test -> Microsoft.NETCore.App (>= 2.0.0)
  Restore failed in 1.26 sec for ~/devel/apps/dotnet/api_test/api_test.csproj.

这是我的 .csproj 文件

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.7" />
    <PackageReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.1" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
  </ItemGroup>

</Project>

-- 更新

正如下面的答案所述,dotnet watch 应该包含在 2.1 版的 SDK 中,所以我做了这个小测试:

$ dotnet --version
2.1.105
$ dotnet new webapi -o tmp_api
The template "ASP.NET Core Web API" was created successfully.
$ cd tmp_api/
$ dotnet watch run
No executable found matching command "dotnet-watch"

--

dotnet watch 从 preview2 版本开始可用(可用 here

dotnet watch 是一个 CLI 工具,而不是您的代码可以依赖的包。 official docs 说明你需要使用 DotNetCliToolReference:

    <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" />

您不应该将其用作 PackageReference

.NET Core 2.1 Preview 2 开始,dotnet watch 等 CLI 工具现在是 SDK 的一部分:

We found that these tools were so popular that having to add them to individual projects didn’t seem like the right design, so we made them part of the SDK.

These tools were previously DotNetCliToolReference tools. They are no longer delivered that way. You can delete the DotNetCliToolReference entries in your project file when you adopt .NET Core 2.1.