gRPC - Create C# gRPC server

要建立 gRPC 的 Server,須先將 GRPC.Tools、GRPC.Core、Google.Protobuf 這三個 NuGet 套件加入參考。

1
2
3
4
5
6
7
...
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.7.0" />
<PackageReference Include="Grpc.Core" Version="1.20.0" />
<PackageReference Include="Grpc.Tools" Version="1.20.0" />
</ItemGroup>
...


然後設定從 Proto 檔產生需要的程式部分。

1
2
3
4
<ItemGroup>
<Protobuf Include="../../proto/*.proto" GrpcServices="Server" />
<Content Include="@(Protobuf)" LinkBase="" />
</ItemGroup>


編譯後可在 obj 下看到產出的檔案。


接著開始實作 Service。


繼承產出的 Service 基底類別。

1
2
3
...
public class HelloServiceImpl:HelloService.HelloServiceBase
...

並覆寫該服務的方法即可。

1
2
3
4
5
6
...
public override Task<HelloResponse> SayHello(HelloRequest request, ServerCallContext context)
{
...
}
...


程式寫起來會像下面這樣 (這邊筆者只是簡單的將調用時送進來的人名做些加工回傳而已):

1
2
3
4
5
6
7
8
9
10
11
12
13
using System.Threading.Tasks;
using Grpc.Core;

public class HelloServiceImpl:HelloService.HelloServiceBase
{
public override Task<HelloResponse> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloResponse
{
Name = "Hello~" + request.Name
});
}
}


Service 實作完接著要實作 Server 的部分。


建立 Grpc.Core.Server 實體。

1
2
3
...
var server = new Grpc.Core.Server();
...


指定 Service 要用哪個類別去處理。

1
2
3
...
server.Services.Add(HelloService.BindService(new HelloServiceImpl()));
...


指定 Server 的位置與 Port。

1
2
3
...
server.Ports.Add(new ServerPort(host, port, ServerCredentials.Insecure));
...


啟動 Server。

1
2
3
...
server.Start();
...


等待終止訊號,最後停止 Server 即可。

1
2
3
...
server.ShutdownAsync();
...


程式寫起來會像下面這樣:

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
using System;
using Grpc.Core;

namespace GRPC.Server
{
class Program
{
static void Main(string[] args)
{
var host = "127.0.0.1";
var port = 8888;

var server = new Grpc.Core.Server
{
Services = {HelloService.BindService(new HelloServiceImpl())},
Ports =
{
new ServerPort(host, port, ServerCredentials.Insecure)
}
};

server.Start();

Console.WriteLine("GRPC server listening on port " + port);
Console.WriteLine("Press any key to stop the server...");

Console.ReadKey();

server.ShutdownAsync().Wait();
}
}
}


運行起來就可以提供 gRPC 的服務了。