-1

I am building a .NetCore application using gRPC.

I'm having issues constructing code to handle streaming results.

Consider the following protobuf definition:

service SampleService
{
    rpc RegisterClient(G_ClientRequest) returns (stream G_ClientResponse) { }
}

Here is a code snippet to read results from the stream:

public ConnectToClient()
{
    var results = Client.RegisterClient(new G_ClientRequest() 
    { 
        ClientName = "Foo",
        DateTimeStamp = new Google.Protobuf.WellKnownTypes.Timestamp()
    });
    
    var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
    foreach (var result in results.ResponseStream.ReadAllAsync(cancellationToken: cts.Token))
    {

    }   
}

I've been googling around and found something similar to my above code for handling streaming results. However the code does not compile. ReadAllAsync() is not available in the interface definition for IAsyncStreamReader.

Is this functionality not available/supported in .Net Core? I'm using the latest stable version of Grpc (2.38).

If this is not available to me, how do I retrieve results from the ResponseStream in .Net Core?

Thx, JohnB

JohnB
  • 3,921
  • 8
  • 49
  • 99
  • 1
    Third-party, but protobuf-net.Grpc exposes the regular gRPC APIs using IAsyncEnumerable-T instead, so: `await foreach` and you're done – Marc Gravell Jun 21 '21 at 22:48

2 Answers2

1

According to the gRPC docs, interface IAsyncStreamReader does not have a ReadAllAsync() method.

https://grpc.github.io/grpc/csharp/api/Grpc.Core.IAsyncStreamReader-1.html

David Jones
  • 2,879
  • 2
  • 18
  • 23
  • Yes I saw that. Then I also saw some feature requests that were asking for it in addition to some samples that appeared to be using it..., thus the confusion. – JohnB Jun 21 '21 at 22:18
0

ReadAllAsync is an extension included in Grpc.Core, if you have installed the Nuget package Grpc.Net.Client just add:

using Grpc.Core;