첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestSolution.zip
0.01MB

[TestServer 프로젝트]

▶ hello.proto

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

 

728x90

 

▶ HelloService.cs

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
}

 

300x250

 

▶ Program.cs

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

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

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();
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요