ShowProgramCode

2022年8月5日 星期五

C# 連結Golang的Grpc Server

Grpc Client專案要如何設定,請參考之前的文章
這裡主要說明的是如何使用C# .NetCore作為Client連結Golang的Grpc Server。

Golang Grpc Server proto調整

  1. syntax = "proto3"; // 定義要使用的 protocol buffer 版本
  2.  
  3. option csharp_namespace = "GrpcTestApi";
  4.  
  5. package grpcServer; // for name space
  6. //option go_package = "./;grpcServer"; // generated code 的 full Go import path
  7.  
  8. message SumRequest {
  9. repeated int64 input = 1 [packed=true];
  10. }
  11.  
  12. message SumResponse {
  13. int64 result = 1;
  14. }
  15.  
  16. message RemainderRequest{
  17. int64 a = 1;
  18. int64 b = 2;
  19. }
  20.  
  21. message RemainderResponse {
  22. int64 result = 1;
  23. }
  24.  
  25. service grpcService {
  26. rpc Sum(SumRequest) returns (SumResponse) {};
  27. rpc Remainder(RemainderRequest) returns (RemainderResponse) {};
  28. }

連線Grpc Client Help物件

  1. public class GrpcServerConnectHelp
  2. {
  3. private string serverUrl;
  4. private grpcService.grpcServiceClient client;
  5.  
  6. public string ServerUrl { get { return serverUrl; } }
  7.  
  8. public GrpcServerConnectHelp()
  9. {
  10. Init(System.Configuration.ConfigurationManager.AppSettings.Get("GrpcServerUrl"));
  11. }
  12.  
  13. public void Init(string url = null)
  14. {
  15. if (string.IsNullOrEmpty(url))
  16. return;
  17.  
  18. serverUrl = url;
  19. //.NetCore 3.*版本必須加入此行,否則會錯誤。
  20. AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
  21. var channel = GrpcChannel.ForAddress(serverUrl);
  22. client = new grpcService.grpcServiceClient(channel);
  23. }
  24.  
  25. public string CommonApi(string method, string input)
  26. {
  27. string resp = "查無此函式";
  28.  
  29. if (method.Equals("Sum"))
  30. {
  31. return Sum(input);
  32. }
  33.  
  34. if(method.Equals("Remainder"))
  35. {
  36. return Remainder(input);
  37. }
  38.  
  39. return resp;
  40. }
  41.  
  42. private string Sum(string input)
  43. {
  44. dynamic dyn = JsonConvert.DeserializeObject(input);
  45. SumRequest request = new SumRequest();
  46.  
  47. foreach(var value in dyn.Input)
  48. {
  49. long temp = Convert.ToInt64(value);
  50. request.Input.Add(temp);
  51. }
  52. var reply = client.Sum(request);
  53. return reply.Result.ToString();
  54. }
  55.  
  56. private string Remainder(string input)
  57. {
  58. dynamic dyn = JsonConvert.DeserializeObject(input);
  59. RemainderRequest request = new RemainderRequest();
  60. request.A = dyn.A;
  61. request.B = dyn.B;
  62.  
  63. var reply = client.Remainder(request);
  64. return reply.Result.ToString();
  65. }
  66. }

AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

重點在於.Net Core 3.*的版本再傳入URL前,必須加入上面的語法,否則會產生錯誤

沒有留言:

張貼留言