简体   繁体   English

如何处理从CloudPageBlob.BeginUploadFromStream中抛出的异常

[英]How to handle exceptions thrown from within CloudPageBlob.BeginUploadFromStream

I'm using the Azure SDK to upload a stream to an Azure Storage Blob. 我正在使用Azure SDK将流上传到Azure存储Blob。 I want to do it async, so I use CloudPageBlob.BeginUploadFromStream . 我想做异步,所以我使用CloudPageBlob.BeginUploadFromStream Doing this, I started to think about what would happen if an exception is thrown while reading the stream. 这样做,我开始考虑如果在读取流时抛出异常会发生什么。 I couldn't find any information on if the exception would bubble up, so I could catch it with a try catch, or if the method would call the async callback, or what would happen. 我找不到关于异常是否会冒泡的任何信息,所以我可以通过try catch捕获它,或者如果方法将调用异步回调,或者会发生什么。

To try it out, I wrapped my stream in a class that inherits Stream , and the constructor takes my stream as an argument, and keep it as a private variable. 为了试一试,我将流包装在一个继承Stream的类中,构造函数将我的流作为参数,并将其保存为私有变量。 Each inherited method then just call its equal on the private stream. 然后,每个继承的方法只在私有流上调用它的相等。 In the Read() however, I throw an exception, to see what would happen if something goes wrong while reading the stream. 然而,在Read()中,我抛出异常,看看如果在读取流时出现问题会发生什么。 I then pass an instance of my streamwrapper to BeginUploadFromStream() , instead of the actual stream. 然后我将我的streamwrapper实例传递给BeginUploadFromStream() ,而不是实际的流。

It turns out, when an exception is thrown, BeginUploadFromStream() completes and call the async callback, and no exception bubble up to where the method was first called, so I cannot catch it with a try catch. 事实证明,当抛出异常时, BeginUploadFromStream()完成并调用异步回调,并且没有异常冒泡到最初调用方法的位置,所以我无法通过try catch捕获它。 At the same time, I cannot find any info on the IAsyncResult passed to the callback, that an exception was thrown. 同时,我找不到传递给回调的IAsyncResult任何信息,抛出异常。

What can I do to be able to act on the fact that BeginUploadFromStream() didn't complete as expected? 我能做些什么才能对BeginUploadFromStream()未按预期完成的事实采取行动?

我相信当你调用EndUploadFromStream时你会看到异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM