dotnet 核心 PackageReference 与 DotNetCliToolReference [英] dotnet core PackageReference vs DotNetCliToolReference
问题描述
我正在尝试从我的命令行运行 dotnet aspnet-codegenerator
.第一次尝试时,出现错误 No executable found matching command "dotnet-aspnet-codegenerator"
I'm trying to run dotnet aspnet-codegenerator
from my comand line. The first time I tried, I got the error No executable found matching command "dotnet-aspnet-codegenerator"
我意识到我需要将 aspnet-codegenerator
安装为dotnet CLI 工具"(他们的 可扩展性模型 允许添加 CLI 命令,前提是我在 csproj 文件中包含了正确的
元素.)1
I realized I needed to install the aspnet-codegenerator
as "dotnet CLI tool" (part of their extensibility model allows adding CLI commands if I include the correct <DotNetCliToolReference>
element to the csproj file.)1
这个答案告诉我我需要哪个
,即<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1"/>
但它给我留下了几个问题:
This answer tells me which <DotNetCliToolReference>
I need, i.e. <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
but it leaves me with a few questions:
- 我可以使用命令行安装而不是手动编辑吗csproj?
- 我注意到我可以使用
dotnet add 命令安装软件包package
,但添加元素
并且我需要
; - 即运行命令会产生这个(错误的)元素:
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1"/>
- 我注意到我可以使用
- Can I install that using the command line rather than hand-editing
the csproj?
- I notice I can install packages using the command
dotnet add package
, but that adds the element<PackageReference>
and I need<DotNetCliToolReference>
; - i.e. running the command would produce this (wrong) element:
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
- I notice I can install packages using the command
- 我可以将它们添加到同一个
中吗? - 如果我有一个
csproj
,它的第一个也是唯一一个
包含一个
,那么任何后续的dotnet add package
命令失败:error: Invalid restore input.无效的恢复输入.DotnetCliToolReference-BundlerMinifier.Core 输入文件:
. - 我的解决方法是:
- Can I add them to the same
<ItemGroup>
? - When I have a
csproj
whose first and only<ItemGroup>
contains a<DotNetCliToolReference>
, then any subsequentdotnet add package
commands fail:error: Invalid restore input. Invalid restore input. DotnetCliToolReference-BundlerMinifier.Core Input files:
. - My workaround is to:
- 删除任何现有的
DotNetCliToolReference
元素 - 运行
dotnet 添加包
- 完成后,重新添加我删除的内容.
1(我在 Visual Studio Code 中使用最新版本;所以我们使用的是 csproj,而不是 project.json)
1 (I'm in Visual Studio Code and using the latest; so we're using csproj, not project.json)
推荐答案
目前添加
DotNetCliToolReference
项只能通过手动编辑 csproj 文件来实现.该功能在 .NET Core 3.0 中已被弃用,因为它会导致难以调试的奇怪不兼容性.请参阅:https://github.com/dotnet/sdk/issues/3115
At the moment adding
DotNetCliToolReference
items is only possibly by hand-editing the csproj file. The feature was deprecated in .NET Core 3.0, because it caused strange incompatibilities that were difficult to debug. See: https://github.com/dotnet/sdk/issues/3115
逻辑上的区别在于 PackageReference
将成为您的应用程序的一部分 - 您可以使用代码包随附的 dll,它将与您的应用程序一起部署.DotNetCliToolReference
包将从提要中恢复,但不会添加到应用的依赖关系图"中.当 CLI 运行命令时,它还会查看 csproj 文件,以通过 DotNetCliToolReference
项将命令名称解析为相应的 dll 文件.
The logical difference is that PackageReference
s will become part of your application - you can use the dlls shipped with the package from your code and it will be deployed with your app. DotNetCliToolReference
packages will be restored from feeds but not added to your app's "dependency graph". When the CLI runs commands it looks at the csproj file as well to resolve command names to the corresponding dll files through DotNetCliToolReference
items.
这两种项目类型在哪个项目组中并不重要.MSBuild 非常动态,您可以根据需要重新排列文件.CLI 和 NuGet 都使用 MSBuild API 来评估文件并查询项目的项目.
It does not matter in which item groups those two item types are. MSBuild is very dynamic and you can re-arrange the file as you like. Both the CLI and NuGet use MSBuild API to evaluate the file and query the project's items.
当 DotNetCliToolReference
已经存在时,您看到 dotnet add package
失败的错误是一个已为即将发布的 2.0 版本修复的错误:https://github.com/NuGet/Home/issues/4771
The error you are seeing that dotnet add package
fails when a DotNetCliToolReference
is already present is a bug that has been fixed for the upcoming 2.0 release: https://github.com/NuGet/Home/issues/4771
这篇关于dotnet 核心 PackageReference 与 DotNetCliToolReference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!