How to Pass enum In Grpc Request

Viewed 4033

I want to define such a router or Map for my gRPC Server using Enum or similar method

I've a simple Service which named ServerHubService and a Hubs folder which have some classes in it which will be handle every request will be passed by client to my gRPC server

Here is ScreenShot of my Project Project Structure Now as can be seen in the photo The contents of the file are also the image below HubMap.cs

as you can see i want to define a switch case statment to run diffrent classes

Here is my ServiceHubService gRPC class ServerHubService.cs

and finally this my client side call grpc-client.cs

visual studio 2019 ver 16.10

Here is my ServerHub.proto file :

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;
}

my 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)
            }) ;
        }
    }
    
}

and 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"
            };
        
        
    }

}

Problem 2 : Whats this error enter image description here

Question 2:

How to Detect Defined enums in proto file and proccess in HubMap.cs

2 Answers

If you want to pass an enum over gRPC: define the enum in gRPC terms:

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;
}

And use the generated enums. If you don't want to do that and want to use string instead: then all the conversions back and fore are up to you; ToString() and Enum.Parse are your friends.

I would recommend you to take a look at the google protobuf guidelines.

Naming conventions

Designpatterns

The generated Enums will be PascalCase (FOO_BAR => FooBar)

Related