You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core gRPC项目导入google.type.Money类型失败求助

解决ASP.NET Core gRPC中导入google.protobuf.Money失败的问题

以下是无需手动复制proto文件的解决方案:

  • 确保项目安装正确的NuGet包
    除了Grpc.Tools,必须安装Google.Protobuf(包含所有Protobuf标准Well-Known Types的定义),同时保证Grpc.AspNetCore和Grpc.Tools的版本匹配。

  • 检查proto文件的导入语句
    确认导入的是Protobuf标准的Money类型,不要混用Google Cloud的类型:

    import "google/protobuf/money.proto";
    
    // 示例消息定义
    message Order {
      string order_id = 1;
      google.protobuf.Money total_amount = 2;
    }
    
  • 配置项目文件的Protobuf导入目录(按需)
    如果工具仍无法找到proto文件,在项目的.csproj中添加以下配置,让Grpc.Tools能从NuGet包中读取标准proto定义:

    <ItemGroup>
      <Protobuf Include="Protos/**/*.proto" GrpcServices="Server">
        <AdditionalImportDirs>$(NuGetPackageRoot)/google.protobuf/$(GoogleProtobufVersion)/lib/netstandard2.0</AdditionalImportDirs>
      </Protobuf>
    </ItemGroup>
    

    其中$(GoogleProtobufVersion)会自动匹配你安装的Google.Protobuf版本,无需手动填写具体数值。

  • 清理重建项目
    执行以下命令彻底清理后重新构建:

    dotnet clean
    dotnet restore
    dotnet build
    
  • 确认proto文件的生成配置
    在Visual Studio中选中proto文件,右键打开属性面板,确保:

    • 生成操作:Protobuf compiler
    • gRPC服务:根据项目类型选择服务器或客户端和服务器

内容的提问来源于stack exchange,提问作者user8422356

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 14:55:23