从另一个项目导入 .proto 文件 [英] Importing .proto files from another project
问题描述
我有几个包含不同 protobuf 文件的合约项目,但是一些消息类型具有相同的消息类型,例如
I have several contract projects that contains different protobuf files, but some of the message types have the same message type like
message user
{
Address address = 1
}
message Address
{
....
}
我现在已经创建了一个共享项目并向其中添加了一个 Address.proto 文件,其中仅包含
I have now created a shared project and added an Address.proto file to it only containing
syntax = "proto3"
option csharp_namespace = "shared.protos"
package AddressPackage
message Address {....}
我的问题是弄清楚如何将其导入到我不同合约项目中的原型中.我已添加共享项目作为参考,但我从那里尝试的所有其他内容都导致错误.
My problem is to figure out how to import it into the protos in my different contract projects. I have added the shared project as a reference, but everything else that I have tried from there has resultet in errors.
我知道我需要使用 import
只是还没有想出如何编写字符串.
I know that I need to use import
just haven't figured out how to write the string.
更新
我正在使用 gRPC.tools nuget 并且所有 .proto 文件都设置为 protobuf 编译器
I'm using gRPC.tools nuget and all .proto files is set to protobuf compiler both
文件结构如下
User.Contracts 项目
User.Contracts project
- Protos-- 用户.proto共享项目
- Protos-- 地址.proto
两个项目都在它自己的文件夹中,并且这些文件夹彼此相邻.
both projects is in it's own folder and those folders are placed next to each other.
在共享项目中它说
<ItemGroup>
<None Remove="Protos\Address.proto" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="Protos\Address.proto">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Protobuf>
</ItemGroup>
在 user.contract 中说
and in the user.contract is says
<ItemGroup>
<None Remove="Protos\User.proto" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="Protos\User.proto" />
</ItemGroup>
提前致谢.
推荐答案
您可以通过将 ProtoRoot
属性添加到
部分来实现您的 .csproj
文件.
You can do so by adding the ProtoRoot
attribute to the <Protobuf />
section in your .csproj
file.
假设您在项目 A 中有一个 .proto
文件:
Let's say you have a .proto
file in project A:
syntax = "proto3";
option csharp_namespace = "Project.A";
import "ProjectB/<path>/Engine.proto"
message Car {
Engine engine = 1;
...
}
在项目 B 中,您有:
In project B you have:
syntax = "proto3";
option csharp_namespace = "Project.B";
message Engine {
...
}
如您所见,在 car.proto
中,我们使用了来自另一个项目的 .proto
文件的导入语句.为了成功导入这个文件,我们需要在.csproj
文件的
部分添加ProtoRoot
项目A:
As you can see, in car.proto
we used an import statement to a .proto
file from another project. In order to successfully import this file, we need to add ProtoRoot
to the <Protobuf />
section in the .csproj
file of the project A:
<ItemGroup>
<Protobuf Include="ProjectA/<path>/car.proto" Link="<path>/car.proto" ProtoRoot=".." />
</ItemGroup>
相当于 C# 项目中的文件夹结构.ProtoRoot
需要设置为 .proto
文件中的导入声明正在查找文件的目录.在本例中,它是父文件夹,其中包含项目 A 和项目 B 的两个子文件夹.
<path>
is equivalent to your folder structure within your C# project. ProtoRoot
needs to be set to the directory where import declarations in the .proto
files are looking for files. In this case, it's the parent folder which contains two subfolders with project A and project B.
更多有趣的东西可以在这里找到:https://chromium.googlesource.com/external/github.com/grpc/grpc/+/HEAD/src/csharp/BUILD-INTEGRATION.md
More interesting stuff can be found here: https://chromium.googlesource.com/external/github.com/grpc/grpc/+/HEAD/src/csharp/BUILD-INTEGRATION.md
这篇关于从另一个项目导入 .proto 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!