How to clean up after a failed AsyncDuplexStreamingCall?

夙愿已清 提交于 2020-03-05 04:54:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!