首页 > 解决方案 > 将 gRPC proto 文件定义包含到单个 proto buf 文件中时,为什么会出现 C# 编译器错误?

问题描述

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

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

考虑以下 protobuf 文件:

服务.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_WindowsRequest和G_WindowsRequestResponse都引发编译器错误,说明它们未定义。这是csharp_namespace和/或解析的问题吗?

有没有办法在单独的原型定义中强制执行单独的包/csharp_namespace 定义,但在汇总的“协调器”原型文件定义中引用这些定义?

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

2> orchestrator.proto(22,5): error : "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 个错误

标签: asp.net-coregrpcgrpc-c#

解决方案


推荐阅读