0

我不明白为什么只有使用CompleteAsync().

using var call = _apiClient.GetToken(headers: _threadContext.Metadata,  deadline: DateTime.UtcNow.AddSeconds(5));

var keyReq = new GetTokenRequest()
{
    Key = publicKey
};

var readTask = Task.Run(async () =>
{
    await foreach(var message in call.ResponseStream.ReadAllAsync())
    {
        if (message.Challenge != null)
        {
            var challenge = message.Challenge.ToByteArray();
            var signature = await VerifySignature(challenge);
            var signReq = new GetTokenRequest
            {
                Signature = ByteString.CopyFrom(signature)
            };
            await call.RequestStream.WriteAsync(signReq);
            await call.RequestStream.CompleteAsync();
        }
        else if (message.Token != null)
        {
            token = message.Token;
        }
    }
});

await call.RequestStream.WriteAsync(keyReq);

await readTask;

如果我以此更改结尾,我会收到消息,但在响应中下一个WriteAsync失败,因为流已关闭。

await call.RequestStream.WriteAsync(keyReq);
await call.RequestStream.CompleteAsync();
await readTask;

如果我没有完成请求,响应消息永远不会出现。

任何想法 ?

注意:服务器正在运行。

4

1 回答 1

0

This code doesn't work with Grpc.Net.Client.Web only. With classic SocketHttpHandler it's ok. Problem is solved. thanks.

于 2020-10-23T20:42:48.920 回答