Home > front end >  gRPC - Reply with Array
gRPC - Reply with Array

Time:07-12

I'm trying to figure out how to properly pass an array in a server response in gRPC.

In proto (after reading the google documentation) i did:

message HelloReply {
  repeated string message = 1;
}

in service method:

public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
   return Task.FromResult(new HelloReply
   {
      Message = //myArray in foreach with replys = myArray.Length? 
   });
}

What i must to do here? Stream? But what should it look like?

I easily implemented this in the WCF, but with gRPS I ran into a wall. I have not found a basic implementation of such tasks anywhere, except for passing one string (by default in the c# template).

p.s. maybe there is some book with implementation examples in c# or something like that (except for the official documentation)?

CodePudding user response:

I don't have my dev machine on me right now, but this is roughly what you do yo populate a repeated protobuf field. I believe this is what you were after based on the comments in your code.

HelloReply reply = new HelloReply();
foreach(var rep in myArray)
{
   reply.Message.Add(rep);
}
return Task.FromResult(reply);

If this is not what you are after let me know.

CodePudding user response:

Well, If you want to pass the array into gRPC Response, you can probably serialize it into JSON String and pass it as a string to the response and decode it at the destination.

  • Related