如何将一个项目中的原始文件包含到另一个项目中 [英] How to include proto files from one project in another project
问题描述
我无法在项目B中包含项目A中存在的原型.该想法是在项目A和项目B中将 GrpcServices ="Server"
的原型包含在项目A中.测试中包含相同的原型,但现在与 GrpcServices ="Client"
I am not able to include protos present in Project A in a project B. The idea would be to have the protos with GrpcServices="Server"
in project A and in project B, of tests, include the same protos but, now, as GrpcServices="Client"
ProjectA/Protos/Profile.proto
syntax = "proto3";
package profile;
option csharp_namespace = "ProjectA.Protos";
import "google/protobuf/empty.proto";
service ProfileService {
rpc Get(google.protobuf.Empty) returns (Profile);
}
message Profile {
string profile_id = 1;
string description = 2;
}
ProjectA/Protos/User.proto
syntax = "proto3";
package user;
option csharp_namespace = "ProjectA.Protos";
import "google/protobuf/wrappers.proto";
import "Protos/Profile.proto";
service UserService {
rpc Get(google.protobuf.StringValue) returns (UserDetail);
}
message UserDetail {
string id = 1;
string name = 2;
repeated profile .Profile profiles = 7;
}
项目B .csproj(测试项目)
Project B .csproj (The test project)
<ItemGroup>
<Protobuf Include="..\ProjectA\Protos\*.proto" GrpcServices="Client" ProtoRoot="Protos">
<Link>Protos\*.proto</Link>
</Protobuf>
</ItemGroup>
使用这些设置,我总是最终会遇到此错误返回
With these settings I always end up having this error return
error : File does not reside within any path specified using --proto_path (or -I). You must specify a --proto_path which encompasses this file. Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).
推荐答案
为应用的解决方案带来回报.
Bringing a return on the applied solution.
在同一解决方案中尝试执行此导入时出现的问题是, .csproj
中的每个包含都将尝试生成C#类,并且这些类是使用全局上下文"生成的.并且这防止了服务器"的产生.和客户"类,因为它们具有相同的名称.
The problem that occurs when trying to do this import in the same solution is that each inclusion in a .csproj
it will try to generate C# classes and these classes are generated with "global context" and this prevents the generation of "server" and "client" classes because they would have the same names.
最简单的解决方案是将 Both
都添加到原始文件的include配置中.因此,生成的C#类已经在客户端和服务器端进行了设计,因此可以在项目A(服务器)和项目B(客户端)中使用它们.
The easiest solution was to add Both
to the proto file's include configuration. Thus, the generated C# classes already contemplate the client and server side, so it is possible to use them both in project A (server) and in project B (client).
< Protobuf Include ="Protos \ *.proto";GrpcServices =两者"/>
Jan Tattermusch在评论中提出的解决方案将有一个仅包含protos文件的项目C,将采用相同的配置,也是一个很好的选择.
The solution presented by Jan Tattermusch, in the comments, would have a project C with only the protos files, would follow the same configuration and is also a great alternative.
这篇关于如何将一个项目中的原始文件包含到另一个项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!