1

I don't understand why the response comes only if I use 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;

If I change the end with this, I receive messages but in the response the next WriteAsync fails because the stream is closed.

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

And if I doesn't complete the request, response message never comes.

Any idea ?

Note: the server is in go.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • A stream in Net library await blocks until stream is closed. Not when you reach end of stream. The stream could be still filling and you want to wait until all the data is in the stream. – jdweng Oct 23 '20 at 20:20
  • @jdweng If streams blocked until all data is sent, they would be useless compared to byte arrays and impossible to use for sending a lot of data. In reality streams exist specifically to enable processing data in chunks. You must have heard that it is impossible to [cancel a read on a network stream](https://stackoverflow.com/q/12421989/11683), which is a completely different thing. – GSerg Oct 23 '20 at 20:30
  • @GSerg : I did not say the stream was blocked. I was just referring to the await. Look at the ops code. The foreach loops but the loop does not terminate until the stream is closed. – jdweng Oct 23 '20 at 20:34

1 Answers1

0

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

  • I'm facing similar problem. But setting SocketHttpHandler as HttpHandler does not help. Can you provide some code snippet how to solve whis? – Iskander Gabbazov Jul 08 '22 at 08:03