.proto
syntax = "proto3"; import "google/protobuf/timestamp.proto"; option csharp_namespace = "GrpcServerStreaming"; package greet; service Greeter { rpc SayHello (HelloRequest) returns (stream HelloReply); } message HelloRequest { string name = 1; } message HelloReply { string message = 1; google.protobuf.Timestamp timestamp = 2; }
Server
public override async Task SayHello(HelloRequest request, IServerStreamWriter<HelloReply> responseStream, ServerCallContext context) { while (!context.CancellationToken.IsCancellationRequested) { await responseStream.WriteAsync(new HelloReply { Message = String.Format("Hello {0}", request.Name), Timestamp = Timestamp.FromDateTime(DateTime.UtcNow) }); Console.WriteLine(Timestamp.FromDateTime(DateTime.UtcNow)); await Task.Delay(1000); } }
Client
static async Task Main(string[] args) { // The port number(5001) must match the port of the gRPC server. using var channel = GrpcChannel.ForAddress("http://localhost:5000"); var client = new Greeter.GreeterClient(channel); var response = client.SayHello(new HelloRequest {Name = "Mahmood"}); while (await response.ResponseStream.MoveNext(new CancellationToken(false))) { Console.WriteLine(response.ResponseStream.Current.Message); Console.WriteLine(response.ResponseStream.Current.Timestamp); Console.WriteLine("----------------------------------------------------------------"); } Console.ReadKey(); }
References
https://www.youtube.com/watch?v=F2T6xNRoa1E&list=PLUOequmGnXxPOlhyA57ijmEyOeVmYQt32&index=2