【问题标题】:How to Pass enum In Grpc Request如何在 Grpc 请求中传递枚举
【发布时间】:2021-03-30 10:59:32
【问题描述】:

我想使用 Enum 或类似方法为我的 gRPC 服务器定义这样的路由器或映射

我有一个名为 ServerHubService 的简单服务和一个 Hubs 文件夹,其中包含一些类,这些类将处理每个请求将由客户端传递到我的 gRPC 服务器

这是我的项目的屏幕截图 Project Structure 现在可以在照片中看到 文件内容也是下图 HubMap.cs

如您所见,我想定义一个 switch case 语句来运行不同的类

这是我的 ServiceHubService gRPC 类 ServerHubService.cs

最后这是我的客户端调用grpc-client.cs

Visual Studio 2019 版本 16.10

这是我的 ServerHub.proto 文件:

syntax = "proto3";

option csharp_namespace = "NetPlus.Server.Core";

package server;


service ServereHub  {

rpc ActionManager (ActionRequest) returns (ActionResult);

}

message ActionRequest {
    string ActionType = 1;
}

message ActionResult {
    string ActionResultType = 1;
}

我的 ServerHubService.cs :

using Microsoft.Extensions.Logging;
using NetPlus.Server.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NetPlus.Server.Core.Hubs;
namespace NetPlus.Server.Core
{
    public class ServerHubService : ServereHub.ServereHubBase
    {
        private readonly ILogger<ServerHubService> _logger;

       public ServerHubService(ILogger<ServerHubService> logger)
        {
            _logger = logger;
        }

        public override Task<ActionResult> ActionManager(ActionRequest request, ServerCallContext context)
        {
            HubMap map = new HubMap();
            HubMap.HubSelector selector;

         
            return Task.FromResult(new ActionResult
            {
                ActionResultType = map.HubProccessor(selector)
            }) ;
        }
    }
    
}

和 Hubmap.cs

using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NetPlus.Server.Core.Hubs
{
    public class HubMap
    {
        Switcher switcher = new Switcher();

        public string HubNameResult { get; set; }
        public   enum HubSelector
        {
            SwitchServer_Off  = 1

        }
       
        public  string HubProccessor(HubSelector hName) =>
          
        
            hName switch
            {
                HubSelector.SwitchServer_Off => switcher.PutOffline(),
               
                _=> "Error Proccesing  Hub"
            };
        
        
    }

}

问题 2:这是什么错误

问题 2:

如何检测 proto 文件中定义的枚举和 HubMap.cs 中的进程

【问题讨论】:

  • 这里的主要问题似乎只是您没有使用async;那应该是public async Task&lt;string&gt; CustomMethodAsync(...)public async ValueTask&lt;string&gt; CustomMethodAsync(...),不是吗?
  • 不,我得到错误'无法从 HubMap.HubSelector 转换为字符串
  • 您还在ActionManager 中显示了一些由于“明确分配”(selector)造成的损坏代码;如果你能问一个具体的问题,说明你在问什么问题,那就太好了;特别是,说出您要询问的编译器错误(红色波浪线)
  • "不,我得到错误 ' 无法从 HubMap.HubSelector 转换为字符串" - 我们已经猜到了,到底是怎么回事?这正是我说要明确的原因;现在;我们看不到你的ActionRequest 类型,所以......在这里帮助我们; ActionRequest.ActionType 是否定义为 string?并且:应该是吗?
  • 此外,将所有代码显示为问题中的文本,而不是屏幕截图,会有所帮助...

标签: c# grpc


【解决方案1】:

如果您想通过 gRPC 传递枚举:定义 gRPC 术语中的枚举:

syntax = "proto3";

option csharp_namespace = "NetPlus.Server.Core";

package server;

enum ActionType
{   // whatever contents...
    Foo = 0;
    Bar = 1;
    Blap = 2;
}
enum ActionResultType
{
    // ... etc
}
service ServereHub  {
   rpc ActionManager (ActionRequest) returns (ActionResult);
}

message ActionRequest {
    ActionType Action = 1;
}

message ActionResult {
    ActionResultType Result = 1;
}

并使用 生成的 枚举。如果您想要这样做并想改用string:那么所有前后的转换由您决定ToString()Enum.Parse 是你的朋友。

【讨论】:

  • 我需要在枚举之前使用 meesage 关键字吗?
  • @SecGenUnity 没有;枚举可以嵌套在message 中,如果您愿意并且这样做有意义的话,但它们不需要——它们可以是如图所示的顶级。在ActionRequest 内定义ActionType 和在ActionResult 内定义ActionResultType 也是有意义的 - 如果它们不会在其他任何地方使用
【解决方案2】:

我建议您查看 google protobuf 指南。

Naming conventions

Designpatterns

生成的枚举将是 PascalCase (FOO_BAR => FooBar)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 2010-11-05
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多