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.ConsoleApp | Console app | .NET 6.0 | Grpc.Core |
GrpcChatSample.Client.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 |
Project | Type | Target | Used Package |
---|---|---|---|
GrpcChatSample.Server.ConsoleApp | Console app | .NET 6.0 | Grpc.Core |
GrpcChatSample.Server.Wpf | WPF app | .NET Framework 4.8 | Grpc.Core |
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.
- Limited support by Grpc.Net.Client but no sample in this repository.
- ASP.NET Core apps are supported by both, but Grpc.AspNetCore/Grpc.Net.Client is recommended.
- See https://grpc.io/blog/grpc-csharp-future/
- You cannot put proto file in WPF project. See grpc/grpc#20402
Implements
- Simple RPC: Client registers a message to server.
- Server-side streaming RPC: Server streams messages to clients.
- Interceptors: Client puts its ID for each calls. Server checks client IP address and ID for each calls.
- Request Header R/W: Client puts its ID on request header (metadata) of the call.
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
service Chat {
rpc Write(ChatLog) returns (google.protobuf.Empty) {}
rpc Subscribe(google.protobuf.Empty) returns (stream ChatLog) {}
}
DB is not implemented but on-memory List
is used as a placeholder.
- 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.
See these classes. Enable listenOptions.UseHttps()
of server and locate .pfx file. Set var https = true
of client.
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.
- 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.