I wrote a simple function to download a file from SkyDrive into 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;
}
The function fails running the line:
// Download the file
await liveClient.BackgroundDownloadAsync( fileID + "/Content", new Uri( "/shared/transfers/" + filename, UriKind.Relative ) );
With the error: "An exception of type 'System.InvalidOperationException' occurred in mscorlib.ni.dll but was not handled in user code
The request has already been submitted"
The function succeeds if I modify the line to (removing the await):
// Download the file
liveClient.BackgroundDownloadAsync( fileID + "/Content", new Uri( "/shared/transfers/" + filename, UriKind.Relative ) );
Why is that?
Thx
There is a need to check if BackgroundTransferService.Request is empty and if not to remove any pending request.
I modified my code like this, and it seems to work fine:
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;
}