为什么LiveConnectClient.BackgroundDownloadAsync在尝试“;等待”;它

本文关键字:等待 LiveConnectClient BackgroundDownloadAsync 为什么 | 更新日期: 2023-09-27 18:22:35

我编写了一个简单的函数,将文件从SkyDrive下载到IsolatedStorage中。

    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 ) );

出现错误:"mscorlib.ni.dll中发生类型为System.InvalidOperationException的异常,但未在用户代码中处理

请求已提交"

如果我将行修改为(删除等待),则功能成功:

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

为什么?

Thx

为什么LiveConnectClient.BackgroundDownloadAsync在尝试“;等待”;它

需要检查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;
    }