简体   繁体   English

如何在 .Net Core 中使用 Grpc 检索流结果?

[英]How do I retrieve streaming results using Grpc in .Net Core?

I am building a .NetCore application using gRPC.我正在使用 gRPC 构建一个 .NetCore 应用程序。

I'm having issues constructing code to handle streaming results.我在构建代码来处理流结果时遇到问题。

Consider the following protobuf definition:考虑以下 protobuf 定义:

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 . ReadAllAsync()IAsyncStreamReader的接口定义中不可用

Is this functionality not available/supported in .Net Core?此功能在 .Net Core 中不可用/不支持吗? I'm using the latest stable version of Grpc (2.38).我正在使用 Grpc (2.38) 的最新稳定版本。

If this is not available to me, how do I retrieve results from the ResponseStream in .Net Core?如果这对我不可用,我如何从 .Net Core 的ResponseStream中检索结果?

Thx, JohnB谢谢,约翰B

According to the gRPC docs, interface IAsyncStreamReader does not have a ReadAllAsync() method.根据 gRPC 文档,接口 IAsyncStreamReader 没有 ReadAllAsync() 方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM