首页 > 解决方案 > 如何在 AspNet Core gRPC Service 中导入 protobuf 知名类型

问题描述

我,对如何将“google/protobuf/empty.proto”导入我的原型文件而摸不着头脑。到目前为止我在做什么:

  1. 在我的 .csproj 文件中,我添加了以下内容:

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
       <PropertyGroup>
          <TargetFramework>netcoreapp3.0</TargetFramework>
       </PropertyGroup>
    
       <ItemGroup>
          <Protobuf Include="C:\Users\me\.nuget\packages\google.protobuf.tools\3.10.0\tools\google\protobuf\*.proto" GrpcServices="Server"/>
          <Protobuf Include="Protos\*.proto" GrpcServices="Server" />
       </ItemGroup>
    
       <ItemGroup>
          <PackageReference Include="Grpc.AspNetCore" Version="2.23.2" />
          <PackageReference Include="System.Reactive" Version="4.1.6" />
       </ItemGroup>
    
    </Project>
    
  2. 在我的项目文件夹中,我有一个名为“Protos”的文件夹,其中有一个 test.proto 文件,我想在其中使用“empty.proto”,例如:

import "google/protobuf/empty.proto";

但是,我得到了错误File not found in project。我也不喜欢在ItemGroup. 使用google protobuf知名类型的正确方法是什么?

标签: c#asp.net-coreprotocol-buffersgrpc

解决方案


如果您使用的是 resharper,请按照图片

转到 r# 选项

取消选中选定的 protobuf

这个问题的原因:虽然 Protobuf 完全基于路径,并且一旦在硬盘上找到文件 ReSharper 需要将所有文件添加到解决方案中才能处理它们,并且当前实现的包含文件在 protobuf 支持中的搜索不允许在解决方案之外查找文件。

玩得开心


推荐阅读