问题
I have a AsyncDuplexStreamingCall that fails on the server side at the first write to the request stream. This will result in an RpcException at that point on the client side, as I'd expect. Today, however, I noticed an UnobservedTaskException and figured out that I also have to ensure that the response stream is not left in an error state:
var call = _client.HelloWorld();
try
{
await call.RequestStream.WriteAsync(...); // fails with an RpcException, as expected
...
}
catch (RpcException)
{
// without the following line you get an UnobservedTaskException
// this means you cannot just do using (var call ...) { ... }
await call.ResponseStream.MoveNext().IgnoreUnobservedException();
throw;
}
finally
{
call.Dispose();
}
My question is, am I missing anything in the API that makes this safer and less clunky?
Right now I am a bit concerned that I am missing non-obvious exceptions that will lead to unobserved exceptions in the end.
Would it be possible for the Api to throw an AggregateException for all things that are in a faulty state at the time of disposal so that no unobserved exceptions are left behind?
Thanks!
来源:https://stackoverflow.com/questions/60322037/how-to-clean-up-after-a-failed-asyncduplexstreamingcall