Bug 27862 - How to handle the AmazonServiceException thrown when disconnected with network
Summary: How to handle the AmazonServiceException thrown when disconnected with network
Alias: None
Product: Components
Classification: Xamarin
Component: Xamarin Components ()
Version: Production (addons.xamarin.com)
Hardware: All All
: --- normal
Target Milestone: ---
Assignee: Bill Holmes
Depends on:
Reported: 2015-03-10 22:26 UTC by quang.tran@sioux.asia
Modified: 2015-05-19 12:16 UTC (History)
5 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description quang.tran@sioux.asia 2015-03-10 22:26:15 UTC
I'm using AWS mobile SDK for Xamarin (http://aws.amazon.com/about-aws/whats-new/2014/10/09/aws-mobile-sdk-for-xamarin-available-on-awslabs/) and i got a problem when i use GetObjectAsync() method to get files from my S3 Bucket. My code:

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;

The problem is that when the application waiting for the response from GetObjectAsync() method, the phone is disconnected with the network then i got an exception:

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

And my application is crashed. I tried to handle the application by using ExceptionEvent but i still cannot catch this exception.

Is there any suggestion to solve this problem? (It's very terrible that the application crashes each time the wifi disconnected).
Comment 1 Rui Marinho 2015-05-19 12:16:16 UTC
Sorry but this isn't a Forms related question. But as a recommendation you can wrap that call to GetObjectAsync() in a try/catch block and handle  AmazonServiceException.