【问题标题】:Why LiveConnectClient.BackgroundDownloadAsync fails if trying to "await" it?如果尝试“等待”它,为什么 LiveConnectClient.BackgroundDownloadAsync 会失败?
【发布时间】:2013-07-18 03:46:05
【问题描述】:

我编写了一个简单的函数来将文件从 SkyDrive 下载到独立存储中。

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

这是为什么呢?

谢谢

【问题讨论】:

  • “失败”是什么意思?会死机吗?有例外吗?
  • 我得到的错误是:“在 mscorlib.ni.dll 中发生'System.InvalidOperationException'类型的异常,但未在用户代码中处理请求已提交”
  • 您的代码是否多次提交该请求?
  • 请看我对这个问题的回答。但是,您是对的,“不知何故”不止一个请求。

标签: c# windows-phone-8 async-await onedrive live-sdk


【解决方案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;
    }

【讨论】:

    猜你喜欢
    • 2020-01-23
    • 1970-01-01
    • 2022-09-23
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 2012-06-28
    相关资源
    最近更新 更多