简体   繁体   English

从不同目录导入 proto 文件

[英]Importing proto files from different directories

I'm struggling writing the right configuration for grpc imports.我正在努力为 grpc 导入编写正确的配置。

So the .net solution structure is like:所以 .net 解决方案结构如下:
\Protos\Common\common.proto \Protos\Common\common.proto
\Protos\Vehicle\car.proto \Protos\车辆\car.proto
\CarMicroservice \汽车微服务

Inside car.proto I have: import "common.proto"在 car.proto 里面我有: import "common.proto"

What I want is the generated grpc code to be inside the project CarMicroservice.我想要的是生成的 grpc 代码在项目CarMicroservice 中。
Inside CarMicroservice.csproj I have written the path to the protos:在 CarMicroservice.csproj 里面,我写了原型的路径:

<Protobuf Include="common.proto" ProtoRoot="..\Protos\Common\" />
<Protobuf Include="car.proto" ProtoRoot="..\Protos\Vehicle\" />

But getting error: 'common.proto' no such file or directory但出现错误:'common.proto' 没有这样的文件或目录

The question is: How do I correctly import common.proto inside the car.proto?问题是:如何在 car.proto 中正确导入 common.proto?

Note: I already looked at the similar issue, but couldn't make it work for my case注意:我已经看过类似的问题,但无法使其适用于我的案例
Importing.proto files from another project 从另一个项目导入.proto文件

Ok, I finally solved the issue.好的,我终于解决了这个问题。 Also @DazWilkin pointed it out. @DazWilkin 也指出了这一点。

  1. You can't use relative paths in the import , so you should use absolute path of the project.您不能在import中使用相对路径,因此您应该使用项目的绝对路径。 In my case it was: import "Common/common.proto"在我的例子中是: import "Common/common.proto"
  2. Use the project root for the location of proto files.使用项目根目录作为 proto 文件的位置。 So instead of ProtoRoot="..\Protos\Common\" use ProtoRoot="../Protos/"因此,而不是ProtoRoot="..\Protos\Common\"使用ProtoRoot="../Protos/"
    Now comes the interesting part.现在到了有趣的部分。
    For some reason when I used backslashes for the ProtoRoot path as "..\Protos\出于某种原因,当我使用 ProtoRoot 路径的反斜杠作为"..\Protos\
    I was getting errors as 'file not found'.我收到“找不到文件”的错误。 So don't use the backslashes for paths.所以不要对路径使用反斜杠。 The final code in CarMicroservice.csproj is like the following: CarMicroservice.csproj 中的最终代码如下所示:
<Protobuf Include="Common/common.proto" ProtoRoot="../Protos/" />
<Protobuf Include="Vehicle/car.proto" ProtoRoot="../Protos/" />

Alternatively, there are two other ways to specify include directories for protoc .或者,还有其他两种方法可以为protoc指定包含目录。

First, there is a global property named Protobuf_AdditionalImportsPath .首先,有一个名为Protobuf_AdditionalImportsPath的全局属性。 If you specify this property in your .csproj / .fsproj , it will be passed on to protoc for every .proto in that project.如果您在.csproj / .fsproj中指定此属性,它将传递给该项目中每个.protoprotoc You can use it like this:你可以像这样使用它:

<PropertyGroup>
    <Protobuf_AdditionalImportsPath>../../my/other/directory</Protobuf_AdditionalImportsPath>
</PropertyGroup>

There is also a AdditionalImportDirs property you can specify directly on <Protobuf> elements.还有一个AdditionalImportDirs属性,您可以直接在<Protobuf>元素上指定。 This will likely only be valid for the .proto s you specify it on:这可能只对您指定的.proto有效:

<Protobuf Include="MyProto.proto" Link="MyProto.proto" AdditionalImportDirs="../../my/other/directory" />

Keep in mind that in both cases, you can specify any directory you want, regardless of whether it is a parent of the .proto you're compiling.请记住,在这两种情况下,您都可以指定您想要的任何目录,而不管它是否是您正在编译的.proto的父目录。

Also keep in mind that the import path you specify in your .proto needs be relative to one of the import directories you specify.另请记住,您在.proto中指定的导入路径需要相对于您指定的导入目录之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM