A light-weight RPC wrap of google gRPC framework.
Package | NuGet |
---|---|
SimpleRpc.Shared | |
SimpleRpc.Server | |
SimpleRpc.Client |
// gRPC Client side
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false, true)
.Build();
var provider = new ServiceCollection()
.AddRpcClient(configuration)
.AddRpcClientService<IUserService>()
.BuildServiceProvider();
// gRPC Client side
var tokenSource = new CancellationTokenSource(1000 * 60 * 2);
var userService = provider.GetService<IUserService>();
var userDto = await userService.GetUserBy(userRequest, tokenSource.Token);
// gRPC Client side
var tokenSource = new CancellationTokenSource(1000 * 60 * 2);
var rpcChannel = provider.GetService<IRpcChannel>();
var call = rpcChannel.AsyncClientStreamingCall<UserDto, UserDto>("greet.Greeter", "TestClientStreaming", tokenSource.Token);
await call.RequestStream.WriteAsync(new UserDto { Id = 1, Name = "abc1" });
await call.RequestStream.WriteAsync(new UserDto { Id = 2, Name = "abc2" });
await call.RequestStream.CompleteAsync();
var userDto = await call;
Console.WriteLine($"Id: {userDto.Id}, Name: {userDto.Name}, CreateDate: {userDto.CreateDate:yyyy-MM-dd HH:mm:ss fff}");
// gRPC Server side
- see
IUserService.cs
line: 19-20 - see
UserService.cs
line: 36-50
// gRPC Client side
var tokenSource = new CancellationTokenSource(1000 * 60 * 2);
var rpcChannel = provider.GetService<IRpcChannel>();
var call = rpcChannel.AsyncServerStreamingCall<UserRequest, UserDto>("greet.Greeter", "TestServerStreaming", new UserRequest { Id = 1, Keyword = $"client[ServerStreaming]1: {input}" }, tokenSource.Token);
await call.ResponseStream.MoveNext(tokenSource.Token).ConfigureAwait(false);
var userDto = call.ResponseStream.Current;
Console.WriteLine($"ServerStreaming: Id: {userDto.Id}, Name: {userDto.Name}, CreateDate: {userDto.CreateDate:yyyy-MM-dd HH:mm:ss fff}");
await call.ResponseStream.MoveNext(tokenSource.Token).ConfigureAwait(false);
userDto = call.ResponseStream.Current;
Console.WriteLine($"ServerStreaming: Id: {userDto.Id}, Name: {userDto.Name}, CreateDate: {userDto.CreateDate:yyyy-MM-dd HH:mm:ss fff}");
// gRPC Server side
- see
IUserService.cs
line: 22-23 - see
UserService.cs
line: 52-76
var tokenSource = new CancellationTokenSource(1000 * 60 * 2);
var rpcChannel = provider.GetService<IRpcChannel>();
var call = rpcChannel.AsyncDuplexStreamingCall<UserRequest, UserDto>("greet.Greeter", "TestDuplexStreaming", tokenSource.Token);
await call.ResponseStream.MoveNext(tokenSource.Token).ConfigureAwait(false);
var userDto = call.ResponseStream.Current;
Console.WriteLine($"DuplexStreaming: Id: {userDto.Id}, Name: {userDto.Name}, CreateDate: {userDto.CreateDate:yyyy-MM-dd HH:mm:ss fff}");
await call.ResponseStream.MoveNext(tokenSource.Token).ConfigureAwait(false);
userDto = call.ResponseStream.Current;
Console.WriteLine($"DuplexStreaming: Id: {userDto.Id}, Name: {userDto.Name}, CreateDate: {userDto.CreateDate:yyyy-MM-dd HH:mm:ss fff}");
await call.RequestStream.WriteAsync(new UserRequest { Id = 1, Keyword = $"client[DuplexStreaming]1 - {input}" });
await call.RequestStream.WriteAsync(new UserRequest { Id = 2, Keyword = $"client[DuplexStreaming]2 - {input}" });
await call.RequestStream.CompleteAsync();
// gRPC Server side
- see
IUserService.cs
line: 25-26 - see
UserService.cs
line: 78-107
-
Unary supported.
-
ClientStreaming supported.
-
ServerStreaming supported.
-
DuplexStreaming supported.