2013-07-17 39 views
1

我寫了一個簡單的函數來將文件從SkyDrive下載到IsolatedStorage中。爲什麼LiveConnectClient.BackgroundDownloadAsync嘗試「等待」它失敗?

public static async Task<T> DownloadFileData<T>(string fileID, string filename) 
     { 
     var liveClient = new LiveConnectClient(Session); 

     // Download the file 
     await liveClient.BackgroundDownloadAsync(fileID + "/Content", new Uri("/shared/transfers/" + filename, UriKind.Relative)); 

     // Get a reference to the Local Folder 
     string root = ApplicationData.Current.LocalFolder.Path; 
     var storageFolder = await StorageFolder.GetFolderFromPathAsync(root + @"\shared\transfers"); 

     // Read the file 
     var FileData = await StorageHelper.ReadFileAsync<T>(storageFolder, filename); 
     return FileData; 
     } 

函數失敗運行線路:

// Download the file 
await liveClient.BackgroundDownloadAsync(fileID + "/Content", new Uri("/shared/transfers/" + filename, UriKind.Relative)); 

出現錯誤: 「‘System.InvalidOperationException’類型的異常出現在mscorlib.ni.dll但在用戶代碼中沒有處理

請求已提交」如果我修改線(去除的await)

函數成功:

// Download the file 
liveClient.BackgroundDownloadAsync(fileID + "/Content", new Uri("/shared/transfers/" + filename, UriKind.Relative)); 

這是爲什麼?

THX

+3

你是什麼意思「失敗」嗎?它是否陷入僵局?是否有例外? –

+0

我得到的錯誤是: 「在mscorlib.ni.dll中發生類型爲'System.InvalidOperationException'的異常,但未在用戶代碼中處理 請求已提交」 –

+1

您的代碼是否提交該請求超過一旦? –

回答

1

有必要檢查BackgroundTransferService.Request是空的,如果不刪除任何未處理的請求。

我修改了這樣的代碼,它似乎很好地工作:

public static async Task<T> DownloadFileData<T>(string skydriveFileId, string isolatedStorageFileName) 
    { 
    var liveClient = new LiveConnectClient(Session); 

    // Prepare for download, make sure there are no previous requests 
    var reqList = BackgroundTransferService.Requests.ToList(); 
    foreach (var req in reqList) 
     { 
     if (req.DownloadLocation.Equals(new Uri(@"\shared\transfers\" + isolatedStorageFileName, UriKind.Relative))) 
      { 
      BackgroundTransferService.Remove(BackgroundTransferService.Find(req.RequestId)); 
      } 
     } 

    // Download the file into IsolatedStorage file named @"\shared\transfers\isolatedStorageFileName" 
    try 
     { 
     await liveClient.BackgroundDownloadAsync(skydriveFileId + "/Content", new Uri(@"\shared\transfers\" + isolatedStorageFileName, UriKind.Relative)); 
     } 
    catch (TaskCanceledException exception) 
     { 
     Debug.WriteLine("Download canceled: " + exception.Message); 
     } 

    // Get a reference to the Local Folder 
    var storageFolder = await GetSharedTransfersFolder<T>(); 

    // Read the file data 
    var fileData = await StorageHelper.ReadFileAsync<T>(storageFolder, isolatedStorageFileName); 
    return fileData; 
    } 
相關問題