[英]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 也指出了这一点。
import
, so you should use absolute path of the project.import
中使用相对路径,因此您应该使用项目的绝对路径。 In my case it was: import "Common/common.proto"
import "Common/common.proto"
ProtoRoot="..\Protos\Common\"
use ProtoRoot="../Protos/"
ProtoRoot="..\Protos\Common\"
使用ProtoRoot="../Protos/"
"..\Protos\
"..\Protos\
<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
中指定此属性,它将传递给该项目中每个.proto
的protoc
。 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.