is it possible to return a variable and stream from Grpc Server

Viewed 20

I wanted to know if its possible to stream events and also return a response from the same method in Grpc server call from the client. My rpc looks like this:

rpc StreamEvents (TestUnitRequest) returns (stream Events);

this rpc method is used to stream events and below rpc method is used to subscribe to events ,load program api and log timestamp values and return a boolean value on successful load. rpc SendStatus(AppStatus) returns (CommandReply);

The client sends a request to load data and stream events from the Grpc server in the following way:

var reply1 = client.SendStatus(new AppStatus {});
ExceptionMethod(reply1);
                   
var result = client.StreamEvents( new TestUnitRequest { });

I am trying to write the streamed events to file and return boolean value to the console as:

Console.WriteLine(reply1.IsAccepted);

Console.WriteLine("Streaming events.....");
StringBuilder sb6 = new StringBuilder();
foreach (var cmd in result.Events)
{
  sb6.Append(cmd);
  File.AppendAllText(filePath11 + "SampleTpEventlog.txt", sb6.ToString());
}
sb6.Clear();

But the streaming is returning null value. Here is the server side code for rpc method sendStatus:

disposable = service.SubscribeToEvents(OnEvents);
try
{
  var val1 = await service.LoadTestProgram(appstatus.Dir, appstatus.Tpl, appstatus.Stpl, appstatus.Soc, appstatus.Xml, appstatus.Env, true, true);
  // Console.WriteLine(now.ToString());

  retval = val1;

}
catch (Exception e)
{
 errmsg = e.Message;
}


return new CommandReply
{
  IsAccepted = retval               
};

server side code for rpc method StreamEvents is:

public override async Task StreamEvents(TestUnitRequest tur, IServerStreamWriter<Events> responseStream, ServerCallContext context)
{
  await responseStream.WriteAsync(Events);
}

Please let me know if this is enough.Server is on remote desktop and client on local desktop.

0 Answers
Related