在 Visual Studio/Rider 中使用 proto 文件中的导入

Pat*_*ick 2 protocol-buffers protobuf-net visual-studio proto rider

在 .proto 文件中使用导入时出现“找不到文件”错误。我正在使用 Rider,但在使用 Visual Studio 时遇到了同样的问题。

第一个原型文件:

syntax = "proto3";

import "/fileToImport.proto";

service GreeterAPI {
  rpc SayHello (SayHelloRequest) returns (SayHelloResponse);
}

message SayHelloRequest {
  string name = 1;
}

message SayHelloResponse {
  string answer = 1;
}
Run Code Online (Sandbox Code Playgroud)

我要导入的第二个 proto 文件:

syntax = "proto3";

message Foo {
  string bar = 1;
}
Run Code Online (Sandbox Code Playgroud)

这两个文件在项目目录中彼此相邻。

.csprjo 文件:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="Google.Protobuf" Version="3.10.1" />
      <PackageReference Include="Grpc.Core" Version="2.25.0" />
      <PackageReference Include="Grpc.Tools" Version="2.25.0" />

      <Protobuf Include="**/*.proto" />
    </ItemGroup>

</Project>
Run Code Online (Sandbox Code Playgroud)

如果我在没有导入行的情况下构建项目,一切都很好。但是使用导入行我得到“找不到文件”

我知道我可以使用--proto_path告诉 protoc 所有文件。但我不想构建额外的预构建脚本或类似的东西。我想使用构建来支持 IDE。

Ale*_*.C. 7

我和你有同样的问题,对我有用的修复是将 .proto 文件的包含文件夹添加到导入中。假设两个 .proto 文件都在“Protos”文件夹中,请尝试更改

import "/fileToImport.proto";import "Protos/fileToImport.proto"

还尝试从 .csproj 文件中更改

<Protobuf Include="**/*.proto" />

<ItemGroup> <Protobuf Include="Protos/includingFile.proto" Link="includingFile.proto"/> <Protobuf Include="Protos/fileToInclude.proto" Link="fileToInclude.proto"/> </ItemGroup>

希望有帮助