首页 > 解决方案 > NuGet 混乱 - 你能赢得比赛吗

问题描述

环境:Macos 上的 Powershell 7

我正在尝试安装grpc包:

install-Package Grpc.Net.Client    

它“永远”挂起,所以我尝试安装

Install-Package Google.Protobuf

然后提出:

Install-Package: Unable to find dependent package(s) (System.Reflection)

好的,然后我尝试安装:

Install-Package System.Reflection                                                                                              

然后它提出:

WARNING: 'System.Reflection' matched package 'System.Reflection/4.3.0' from provider: 'NuGet', source 'System.Reflection'.
WARNING: 'System.Reflection' matched package 'System.Reflection/4.1.0' from provider: 'NuGet', source 'nuget.org'.
Install-Package: Unable to install, multiple packages matched 'System.Reflection'. Please specify a single -Source.

然后我尝试:

 Install-Package System.Reflection -Source nuget.org

然后它提出:

The package(s) come(s) from a package source that is not marked as trusted.
Are you sure you want to install software from 'nuget.org'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): 

然后我尝试:

Register-PackageSource -Name System.Reflection -Provider Nuget

然后我得到:

Register-PackageSource: Package Source 'System.Reflection' exists.

然后我尝试......:

Well, I really dont know how this packagemanager can help me?

标签: powershellnugetnuget-package

解决方案


tl;dr: 使用dotnet add package Grpc.Net.Client,或者 Visual Studio for Mac 中存在的任何 NuGet 功能,如果你使用它的话。

更长的版本:NuGet 的Install-Packagepowershell cmdlet文档说:

本主题介绍 Windows 上 Visual Studio 中包管理器控制台中的命令。有关通用 PowerShell Install-Package 命令,请参阅 PowerShell PackageManagement 参考。

Powershell 的Install-Packagecmdlet文档没有定义或链接到他们认为的“软件包”,但是,我的理解是 powershell 包向 Powershell 添加了新的 Powershell 命令。

因此,PowershellInstall-Package不适用于将 NuGet 包安装到 .NET 项目中,但 Powershell 模块可以作为 NuGet 包分发。NuGetInstall-Package仅在 Windows 上的 Visual Studio 中可用。这是一个非常不幸的名字冲突,如果不破坏很多人就无法修复,所以坦率地说,我无法想象它会被改变。

您链接到的 GRPC 示例可能不够清楚地表明 powershell 命令仅适用于 Visual Studio for Windows。我能看到的对 powershell 的唯一引用是代码块上方的一个小标题,表示代码块的内容是 powershell,但有关代码块的说明明确表示选择我认为仅存在于 Visual Studio for Windows 中的菜单.

许多 Microsoft 文档页面的底部都有评论部分,但不幸的是,您链接的示例没有,因此我将代表您创建一个问题,要求文档维护人员对其进行改进。

如顶部的 tl;dr 语句中所述,如果您的项目是 SDK 样式的项目,您可以使用 dotnet CLI 将包添加到您的项目:dotnet add package <package_id>.


推荐阅读