为什么在将 gRPC proto 文件定义包含到单个 proto buf 文件中时会出现 C# 编译器错误?

问题描述

我正在为 .NetCore gRPC 应用程序构建一系列 ProtoBuf 定义。 我正在使用 orchestrator 模式,因为我将创建一系列微服务。

我的 proto 定义遇到了问题,将编译器错误视为“未定义”。 定义位于几个不同的文件中(具有不同的 C# 命名空间)。 以下是我定义的简化版本。

考虑以下 protobuf 文件

service.proto

Syntax = "proto3";
option csharp_namespace = "RequestHandlerService";
package test;

service Handler
{
  rpc AddItem1(G_RequestType1) returns (G_RequestResponse) { }
  rpc AddItem2(G_RequestType2) returns (G_RequestResponse) { }
}

message G_RequestType1{
  string originId = 1;
  string request = 2;
}

message G_RequestType2{
  string originId = 1;
  string request = 2;
}

message G_RequestResponse{
  string response = 1;
}

windowsservice.proto:

Syntax = "proto3";
import "google/protobuf/timestamp.proto";
option csharp_namespace = "Windows.RequestHandler.Service";
package windows.requesthandler.service;

service WindowsHandler
{
    rpc Action1(G_Action1Request) returns (G_Action1Response) { }
    rpc Action2(G_Action2Request) returns (G_Action2Reponse) { }
}

message G_Action1Request{
    string transactionId = 1;
    google.protobuf.Timestamp DateTimeStamp = 2;
    string filePath = 3;
}

message G_Action2Request{
    string transactionId = 1;
    google.protobuf.Timestamp DateTimeStamp = 2;
    string textToInsert = 3;
}

message G_Action1Response {
    google.protobuf.Timestamp DateTimeStamp = 1;
    string fileSecurityStatusMessage = 2;
    G_Action1Request action1Request = 3;
}

message G_Action2Response {
    google.protobuf.Timestamp DateTimeStamp = 1;
    string fileModifierStatusMessage = 2;
    G_Action2Request action2Request = 3;
}


message G_WindowsRequest {
    int32 requestId = 1;
    oneof RequestType{
        G_Action1Request action1Request = 2;
        G_Action2Request action2Requestion = 3;
    }
}

message G_WindowsRequestResponse {
    bool wasSuccessful = 1;
    string response = 2;
    oneof ResponseType{
        G_Action1Response action1Response = 3;
        G_Action2Response action2Respone = 4;
    }
}

orchestrator.proto

Syntax = "proto3";
option csharp_namespace = "orchestratorService";
package test;

import "service.proto";
import "windowsservice.proto";

service orchestrator
{
  rpc RegisterClient(G_RegisterClientRequest) returns (stream G_Response) { }
  rpc EnqueueRequest(G_orchestrationRequest) returns (G_Response) { }
}

message G_orchestrationRequest{
  string originId = 1;
  oneof RequestType{
    G_RequestType1 requestType1 = 2;
    G_RequestType2 requestType2 = 3;
    G_Response response = 4;
    G_WindowsRequest windowsRequest = 5;
    G_WindowsRequestResponse windowsRequestResponse = 6;
  }
}

message G_RegisterClientRequest{
  string clientId = 1;
}

message G_Response{
  bool wasSuccessful = 1;
  string response = 2;
}

这是我的 orchestrator 项目的 csproj 文件中的定义:

 <ItemGroup>
    <Protobuf Include="../Protos/orchestrator.proto" GrpcServices="Server" ProtoRoot="../Protos/">
        <Link>Protos/orchestrator.proto</Link>
    </Protobuf>
    <Protobuf Include="../Protos/service.proto" GrpcServices="Client" ProtoRoot="../Protos/">
        <Link>Protos/service.proto</Link>
    </Protobuf>
    <Protobuf Include="../Protos/windowsservice.proto" GrpcServices="Client" ProtoRoot="../Protos/">
      <Link>Protos/windowservice.proto</Link>
    </Protobuf>
</ItemGroup>

G_WindowsRequestG_WindowsRequestResponse 都抛出编译器错误,指出它们未定义。这是csharp_namespace 和/或 分辨率的问题吗?

有没有办法在单独的 proto 定义中强制执行单独的 package/csharp_namespace 定义,但在汇总的“orchestrator”proto 文件定义中引用这些定义?

这是我看到的编译器错误的片段:

2> orchestrator.proto(22,5):错误:“G_WindowsRequest”不是 定义。 2> orchestrator.proto(23,5): 错误: “G_WindowsRequestResponse”未定义。 2>完成构建目标 项目“Protos.csproj”中的“_Protobuf_CoreCompile”——失败。 2> 2>完成构建项目“Protos.csproj”——失败。 2> 2> 构建失败。 2> 2> orchestrator.proto(22,5): 错误:“G_WindowsRequest”不是 定义。 2>orchestrator.proto(23,5): 错误: “G_WindowsRequestResponse”未定义。 2> 0 警告 2> 2 错误

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)