Search code examples
c#visual-studio-2013windows-runtimewindows-phone-8.1live-sdk

GetAsync (Live SDK) makes its calling method exit when there is no internet connection


I'm having a problem with some of the APIs inside the Live SDK (I'm using the 5.6 version).

This is an example to illustrate my problem:

private async Task MainMethod()
{
    bool result = await UploadStuffOnOneDrive();
    //Do stuff depending on the result...
}

private static async Task<bool> UploadStuffOnOneDrive()
{
    //Get the OneDrive client...
    String folderID = await GetFolderID(client, "TestFolder", "me/skydrive");
    //Do stuff inside the retrieved folder...
}

private static async Task<String> GetFolderID(LiveConnectClient client, String folderName, String directory)
{
    LiveOperationResult opResult;
    opResult = await client.GetAsync(directory + "/files?filter=folders,albums");
    //Return something from opResult...
}

Now, if I have an available network connection, everything runs fine: the UploadStuff method gets the folder ID from GetFolderID, then uploads a file inside that directory and returns.

The problem happens when I don't have a network connection: I tried putting the phone in aero mode and calling MainMethod. When GetFolderID gets called, I thought I'd have thrown an exception if there wasn't an available connection, or that it'd have simply returned null.

Instead, all my methods just disappeared from my StackTrace: GetFolderID exited and all the others did the same. Am I missing something?

How's that even possible for a method to make its calling method exit, without an Exception or something like that? How can I check if my call to GetAsync was successful?

Thanks for your help!

Sergio


Solution

  • Ok, so I figured it out and it looks like that the await call never returns, so the GetAsync method doesn't throw an exception but instead put the calling process on hold for the TaskScheduler, that's why the debugger returns in idle and I see the StackTrace become empty.

    I solved the problem by passing a CancellationTokenSource to the method and awaiting it with:

    LiveOperationResult opResult;
    try
    {
        var awaitableTask = client.GetAsync(directory + "/files?filter=folders,albums");
        var watchedTask = awaitableTask.ContinueWith(task => task.GetAwaiter().GetResult(), cts.Token);
        opResult = await watchedTask;
    catch
    {
         throw new OperationCanceledException("Operation canceled by the user");
    }