从流中读取时,Grpc服务器中未处理异常



我制作了一个简单的应用程序,其中文件块从客户端流式传输到服务器。服务器端我已经处理了异常,这样我自己的响应就会返回到客户端。然而,当我在从流读取完成之前抛出异常时,即使它被捕获并返回自定义响应,客户端我仍然会得到一个未处理的RpcException,状态为Cancelled

public override async Task<UploadFileResponse> UploadFile(
IAsyncStreamReader<UploadFileRequest> requestStream,
ServerCallContext context)
{
try
{
bool moveNext = await requestStream.MoveNext();
using (var stream = System.IO.File.Create($"foo.txt"))
{
while (moveNext)
{
// If something goes wrong here, before the stream has been fully read, an RpcException
// of status Cancelled is caught in the client instead of receiving an UploadFileResponse of
// type 'Failed'. Despite the fact that we catch it in the server and return a Failed response.
await stream.WriteAsync(requestStream.Current.Data.ToByteArray());
moveNext = await requestStream.MoveNext();
throw new Exception();
}
// If something goes wrong here, when the stream has been fully read, we catch it and successfully return
// a response of our own instead of an RpcException.
// throw new Exception();
}
return new UploadFileResponse()
{
StatusCode = UploadStatusCode.Ok
};
}
catch (Exception ex)
{
return new UploadFileResponse()
{
Message = ex.Message,
StatusCode = UploadStatusCode.Failed
};
}
}

也许我实施这项行动的方式是错误的。我可以理解为什么服务器会返回CancelledRPC异常,因为我们确实在流被完全读取之前取消了调用,但我不明白为什么它会覆盖自定义响应。这两种情况的处理可能都必须在客户端完成——一个失败的响应和一个潜在的RPC异常。

我找到了一些关于"服务器和客户端"主题的材料。

显然,每当出现无效响应时,抛出RpcException是很常见的,正如这里的官方gRPC Github存储库中所示。

最新更新