问题描述
我无法在项目B中包括项目A中存在的原型。其想法是在测试A和项目B中让带有GrpcServices="Server"
的原型包含相同的原型,但是,现在,就像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(测试项目)
<ItemGroup>
<Protobuf Include="..\ProjectA\Protos\*.proto" GrpcServices="Client" ProtoRoot="Protos">
<Link>Protos\*.proto</Link>
</Protobuf>
</ItemGroup>
使用这些设置,我总是最终会遇到此错误返回
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).
解决方法
为应用的解决方案带来回报。
在同一解决方案中尝试执行此导入时出现的问题是,.csproj
中的每个包含都将尝试生成C#类,并且这些类是使用“全局上下文”生成的,这会阻止生成服务器和客户端类的名称,因为它们的名称相同。
最简单的解决方案是将Both
添加到原始文件的包含配置中。因此,生成的C#类已经在客户端和服务器端进行了设计,因此可以在项目A(服务器)和项目B(客户端)中使用它们。
<Protobuf Include="Protos\*.proto" GrpcServices="Both" />
Jan Tattermusch在评论中提出的解决方案将有一个仅包含protos文件的项目C,将采用相同的配置,也是一个很好的选择。