从不同目录导入原型文件 [英] Importing proto files from different directories

查看:115
本文介绍了从不同目录导入原型文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力为grpc导入编写正确的配置.

I'm struggling writing the right configuration for grpc imports.

.net解决方案的结构类似于:
\ Protos \ Common \ common.proto
\ Protos \ Vehicle \ car.proto
\ CarMicroservice

So the .net solution structure is like:
\Protos\Common\common.proto
\Protos\Vehicle\car.proto
\CarMicroservice

在car.proto内部,我有: import"common.proto"

Inside car.proto I have: import "common.proto"

我想要的是生成的grpc代码位于项目内部 CarMicroservice.
在CarMicroservice.csproj内部,我已经编写了原型的路径:

What I want is the generated grpc code to be inside the project CarMicroservice.
Inside CarMicroservice.csproj I have written the path to the protos:

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

但是出现错误:'common.proto'没有这样的文件或目录

But getting error: 'common.proto' no such file or directory

问题是:如何在car.proto内部正确导入common.proto?

The question is: How do I correctly import common.proto inside the car.proto?

注意:我已经研究过类似的问题,但无法使其适用于我的情况
从其他项目导入.proto文件

Note: I already looked at the similar issue, but couldn't make it work for my case
Importing .proto files from another project

推荐答案

好,我终于解决了这个问题.@DazWilkin也指出了这一点.

Ok, I finally solved the issue. Also @DazWilkin pointed it out.

  1. 您不能在 import 中使用相对路径,因此您应该使用项目的绝对路径.就我而言,它是: import"Common/common.proto"
  2. 使用项目根目录作为原型文件的位置.因此,请使用 ProtoRoot =" ../Protos/"
    来代替 ProtoRoot =" .. \ Protos \ Common \" 现在是有趣的部分.
    由于某些原因,当我将ProtoRoot路径的反斜杠用作".. \ Protos \
    我收到找不到文件"错误.因此,请勿使用反斜杠作为路径.CarMicroservice.csproj中的最终代码如下:
  1. You can't use relative paths in the import, so you should use absolute path of the project. In my case it was: import "Common/common.proto"
  2. Use the project root for the location of proto files. So instead of ProtoRoot="..\Protos\Common\" use ProtoRoot="../Protos/"
    Now comes the interesting part.
    For some reason when I used backslashes for the ProtoRoot path as "..\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:

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

这篇关于从不同目录导入原型文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆