GrpcWpfSample
GrpcWpfSample copied to clipboard
gRPC sample WPF/Console apps in C#
Overview
Chat server-client applications as a sample implementation using gRPC for .NET (Grpc.AspNetCore/Grpc.Net.Client) and gRPC for C# (Grpc.Core).
| Project | Type | Target | Used Package |
|---|---|---|---|
| GrpcChatSample.Client.Core | Console app | .NET 6.0 | Grpc.Core |
| GrpcChatSample.Client.Wpf | WPF app | .NET Framework 4.8 | Grpc.Core |
| GrpcChatSample.Server.Core | Console app | .NET 6.0 | Grpc.Core |
| GrpcChatSample.Server.Wpf | WPF app | .NET Framework 4.8 | Grpc.Core |
| GrpcChatSample2.Client.ConsoleApp | Console app | .NET 6.0 | Grpc.Net.Client |
| GrpcChatSample2.Client.Wpf | WPF app | .NET 6.0 | Grpc.Net.Client |
| GrpcChatSample2.Server.ConsoleApp | Console app | .NET 6.0 | Grpc.AspNetCore |
| GrpcChatSample2.Server.Wpf | WPF app | .NET 6.0 | Grpc.AspNetCore |
- These projects are not made from ASP.NET Core gRPC service project template, but adding gRPC service/client on console/WPF app project.
- .NET Framework apps are supported by Grpc.Core only.
- ASP.NET Core apps are supported by both, but Grpc.AspNetCore/Grpc.Net.Client is recommended.
- See https://grpc.io/blog/grpc-csharp-future/
Implements
- Simple RPC: Client registers a message to server.
- Server-side streaming RPC: Server streams messages to clients.
Read the official information
- https://grpc.io/docs/languages/csharp/
- https://grpc.io/docs/guides/
- https://docs.microsoft.com/ja-jp/aspnet/core/grpc/
- https://cloud.google.com/apis/design
RPC Service Definition (chat.proto)
service Chat {
rpc Write(ChatLog) returns (google.protobuf.Empty) {}
rpc Subscribe(google.protobuf.Empty) returns (stream ChatLog) {}
}
Architecture

DB is not implemented but on-memory List is used as a placeholder.
How to Run
- Start a server.
- Start client(s). You can start multiple clients.
- For the console app, write a name and hit enter key first.
- On client, write a message and hit enter key.
- Your message will be registered on server, and you can see it on the client.
How to Enable SSL
grpc-dotnet
See these classes. Enable listenOptions.UseHttps() of server and locate .pfx file. Set var https = true of client.
GrpcCore
See these classes. Locate required files and then change var secure = true; to enable SSL.
| File Name | Description |
|---|---|
| localhost_server.crt | Server's certificate |
| localhost_serverkey.pem | Private key of the server's certificate |
| localhost_client.crt | Client's certificate (optional) |
| localhost_clientkey.pem | Private key of the client's certificate (optional) |
- All these files have to be PEM format without encryption.
- The server has to have the client's certificate or its CA certificate and vice versa. Here you can find an idea about this.
- The sample implementation includes client authentication as well. You can disable it then you don't need client's certificate and its private key.
Remarks
- You have to set "localhost" as Common Name (CN) of the certificate for your test on localhost.
- XCA is the one of the easy way to create the these files.