Visual Studio 2019中C#项目无法找到Grpc.Tools引用的问题
在Visual Studio 2019中使用NuGet安装gRPC包的问题
问题描述
- 已通过NuGet包管理器安装
Grpc.Tools包并勾选解决方案中的该包,但代码中引用using Grpc.Core;时提示找不到引用,项目引用列表中也无任何gRPC相关项。 - 终端执行
NuGet\Install-Package Grpc.Core和Import-Module NuGet命令均报错,不清楚遗漏了哪些安装步骤。
代码示例
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Grpc.Core; namespace gRPCtest { public class Server : Form { public Server() { Text = "Server Window"; Size = new System.Drawing.Size(300, 200); } } public class Client : Form { public Client() { Text = "Client Window"; Size = new System.Drawing.Size(300, 200); } } public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // Create a new instance of the child window Server serverForm = new Server(); serverForm.Show(); } private void button2_Click(object sender, EventArgs e) { // Create a new instance of the child window Client clientForm = new Client(); clientForm.Show(); } } }
终端报错信息
PS C:\Users\Waymo\Desktop\prf.net\gRPCtest> NuGet\Install-Package Grpc.Core NuGet\Install-Package : The module 'NuGet' could not be loaded. For more information, run 'Import-Module NuGet'. At line:1 char:1 + NuGet\Install-Package Grpc.Core + ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (NuGet\Install-Package:String) [], CommandNotFoundException + FullyQualifiedErrorId : CouldNotAutoLoadModule PS C:\Users\Waymo\Desktop\prf.net\gRPCtest> Import-Module NuGet Import-Module : The specified module 'NuGet' was not loaded because no valid module file was found in any module directory. At line:1 char:1 + Import-Module NuGet + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (NuGet:String) [Import-Module], FileNotFoundException + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
问题原因及解决步骤
1. 明确Grpc.Tools的作用
Grpc.Tools仅用于生成gRPC代码,不包含Grpc.Core运行时库,所以安装它不会自动添加Grpc.Core引用,需要单独安装对应包。
2. 正确安装Grpc.Core
方式一:Visual Studio NuGet包管理器
- 右键项目 → 选择「管理NuGet程序包」
- 在「浏览」标签搜索
Grpc.Core,选择后点击「安装」,确保勾选当前项目(而非仅解决方案) - 保留
Grpc.Tools用于代码生成即可
方式二:Visual Studio内置包管理器控制台
打开「工具」→「NuGet包管理器」→「包管理器控制台」,执行:
Install-Package Grpc.Core
(不要用系统PowerShell执行,系统环境默认没有NuGet模块)
3. 解决PowerShell命令报错
系统PowerShell报错是因为默认未集成NuGet模块,若要在系统PowerShell使用:
- 下载NuGet CLI(nuget.exe)到项目目录
- 执行命令:
.\nuget.exe install Grpc.Core -OutputDirectory packages - 手动将packages目录中的
Grpc.CoreDLL添加到项目引用
4. 验证安装
安装完成后,检查项目「引用」列表,确认Grpc.Core存在,此时代码中的using Grpc.Core;即可正常识别。
内容的提问来源于stack exchange,提问作者Mich
相关产品推荐
相关产品推荐

