dotnet核心PackageReference与DotNetCliToolReference [英] dotnet core PackageReference vs DotNetCliToolReference
问题描述
我正在尝试从我的命令行运行 dotnet aspnet-codegenerator
。第一次尝试,出现错误找不到与命令 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工具(其< DotNetCliToolReference> ,我们/ dotnet / articles / core / tools / extensibility rel = noreferrer>扩展性模型允许添加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>
,即< DotNetCliToolReference Include = Microsoft.VisualStudio.Web.CodeGeneration。工具 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添加
来安装软件包。添加元素
package< PackageReference>
,我需要< DotNetCliToolReference>
; - 即运行命令将产生以下(错误)元素:
< 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
- 我可以将它们添加到相同的
< ItemGroup>
吗? - 当我有一个
csproj
时,它的第一个也是唯一的< ItemGroup>
包含< DotNetCliToolReference>
,则任何随后的dotnet添加程序包
命令都会失败:错误:无效的还原输入。无效的还原输入。 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添加软件包
- 完成后,添加我删除的内容。
- remove any existing
DotNetCliToolReference
elements - run
dotnet add package
- After finished, add back what I removed.
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文件来完成,存在功能要求在 https://github.com/NuGet/Home/issues/4901
At the moment adding
DotNetCliToolReference
items is only possibly by hand-editing the csproj file, there is a feature request for a CLI command at https://github.com/NuGet/Home/issues/4901
逻辑上的区别是 PackageReference
s将成为应用程序的一部分-您可以使用程序包附带的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.
您看到的错误是 dotnet添加了当已经存在
失败是已为即将发布的2.0版本修复的错误: https://github.com/NuGet/Home/issues/4771 DotNetCliToolReference
时,软件包
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屋!