1

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' 型の例外が発生しましたが、ユーザー コードで処理されませんでした。

リクエストはすでに送信されています」

行を (await を削除して) 変更すると、関数は成功します。

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

何故ですか?

どうも

4

1 に答える 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;
    }
于 2013-07-17T19:46:05.427 に答える