0

我已经将照片上传到 Facebook 活动页面,主要是按照本教程进行的。到目前为止,结果令人满意,大部分时间都将照片上传到活动页面。但是,有时当程序正在上传照片,并且网络连接延迟或断开时,程序会崩溃并抛出System.AggregateException, 消息:

A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.

通过初步检查,这个的InnerException是:

[Facebook.WebExceptionWrapper]: {"The underlying connection was closed: An unexpected error occurred on a receive."}

, 并且上述异常的 InnerException 是System.IO.IOException, 另一个System.Net.Socket.SocketException在 IOException 中, 其中, 两个错误消息都描述了类似的内容{"An established connection was aborted by the software in your host machine"}

异常的 StackTrace 只给了我:

at System.Threading.Tasks.TaskExceptionHolder.Finalize()

现在很明显,问题是由系统有时可能遇到的糟糕的网络连接引起的。那么,根据本教程引用的代码,我如何或在哪里处理此异常?当这发生在另一个线程中时,我不知道应该将 try-catch 块放在哪里,或者在我看来是这样。

先谢谢了!

教程中的参考代码:

var mediaObject = new FacebookMediaObject
                              {
                                  ContentType = "image/jpeg",
                                  FileName = Path.GetFileName(_filename)
                              }
                              .SetValue(File.ReadAllBytes(_filename));

var fb = new FacebookClient(_accessToken);
fb.PostCompleted += fb_PostCompleted;
fb.PostAsync("/me/photos", new Dictionary<string, object> { { "source", mediaObject } });

public void fb_PostCompleted(object sender, FacebookApiEventArgs e)
{
  if (e.Cancelled)
  {
    var cancellationError = e.Error;
    MessageBox.Show("Upload cancelled");
  }
  else if (e.Error == null)
  {
    // upload successful.
    MessageBox.Show(e.GetResultData().ToString());
  }
  else
  {
    // upload failed
    MessageBox.Show(e.Error.Message);
  }
}
4

0 回答 0