■ GRPC를 사용하는 방법을 보여준다.
[TestServer 프로젝트]
▶ hello.proto
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
syntax = "proto3"; option csharp_namespace = "TestServer"; package hello; // 헬로우 프로토 service HelloProto { rpc SayHello (HelloRequest) returns (HelloReply); } // 헬로우 요청 message HelloRequest { string name = 1; } // 헬로우 응답 message HelloReply { string message = 1; } |
▶ HelloService.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
using Grpc.Core; namespace TestServer.Services; /// <summary> /// 헬로우 서비스 /// </summary> public class HelloService : HelloProto.HelloProtoBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 로거 /// </summary> private readonly ILogger<HelloService> logger; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - HelloService(logger) /// <summary> /// 생성자 /// </summary> /// <param name="logger">로거</param> public HelloService(ILogger<HelloService> logger) { this.logger = logger; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 헬로우 말하기 - SayHello(request, context) /// <summary> /// 헬로우 말하기 /// </summary> /// <param name="request">요청</param> /// <param name="context">컨텍스트</param> /// <returns>응답 태스크</returns> public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) { return Task.FromResult ( new HelloReply { Message = "Hello " + request.Name } ); } #endregion } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using TestServer.Services; var builder = WebApplication.CreateBuilder(args); builder.Services.AddGrpc(); var app = builder.Build(); app.MapGrpcService<HelloService>(); app.MapGet ( "/", () => "gRPC 끝점과의 통신은 gRPC 클라이언트를 통해 이루어져야 합니다." ); app.Run(); |
[TestClient 프로젝트]
▶ hello.proto
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
syntax = "proto3"; option csharp_namespace = "TestClient"; package hello; // 헬로우 프로토 service HelloProto { rpc SayHello (HelloRequest) returns (HelloReply); } // 헬로우 요청 message HelloRequest { string name = 1; } // 헬로우 응답 message HelloReply { string message = 1; } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using Grpc.Net.Client; using TestClient; using var channel = GrpcChannel.ForAddress("http://localhost:5253"); var client = new HelloProto.HelloProtoClient(channel); HelloReply reply = await client.SayHelloAsync(new HelloRequest { Name = "TestClient" }); Console.WriteLine($"REPLY MESSAGE : {reply.Message}"); Console.WriteLine(); Console.WriteLine("PRESS ANY KEY TO EXIT..."); Console.ReadKey(); |