我使用AWS mobile SDK for Xamarin(http://aws.amazon.com/about-aws/whats-new/2014/10/09/aws-mobile-sdk-for-xamarin-available-on-awslabs/),当我使用GetObjectAsync()方法从我的S3 Bucket获取文件时,我遇到了问题。我的代码:
private async Task<string> GetObjectFromAmazonS3(S3Object s3Object, string fileContent, string bucketName)
{
AWSCredentials credential = new BasicAWSCredentials(Constants.S3AccessKey, Constants.S3SecretKey);
_amazonS3Client = new AmazonS3Client(credential, Amazon.RegionEndpoint.EUCentral1);
_amazonS3Client.ExceptionEvent += ClientExceptionHandler;
GetObjectRequest getObjectRequest = new GetObjectRequest
{
BucketName = bucketName,
Key = s3Object.Key
};
GetObjectResponse getObjectResponse = await _amazonS3Client.GetObjectAsync(getObjectRequest);
using (Stream responseStream = getObjectResponse.ResponseStream)
using (StreamReader reader = new StreamReader(responseStream))
{
fileContent = reader.ReadToEnd();
}
Logger.Log(Logger.Category.Debugging, "Download S3 Objects to the phone", "Get object " + s3Object.Key + " successfully");
return fileContent;
}
问题是当应用程序等待来自GetObjectAsync()方法的响应时,手机与网络断开连接然后我得到了一个异常:
2015-03-03T07:23:27.582Z,Debugging,UnhandledException(Android),Amazon.Runtime.AmazonServiceException: A WebException with status NameResolutionFailure was thrown.
---> System.Net.WebException: Error: NameResolutionFailure
==> at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
==> at System.Threading.Tasks.TaskFactory`1[System.Net.WebResponse].InnerInvoke (System.Threading.Tasks.TaskCompletionSource`1 tcs, System.Func`2 endMethod, IAsyncResult l) [0x00000] in <filename unknown>:0
==> --- End of stack trace from previous location where exception was thrown ---
==> at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 ==> at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.WebResponse].GetResult () [0x00000] in <filename unknown>:0
我的应用程序崩溃了。我试图通过使用ExceptionEvent来处理应用程序,但我仍然无法捕获此异常。
有什么建议可以解决这个问题吗? (每次wifi断开连接时应用程序崩溃都非常可怕。)