I am using BackgroundUploadAsync
API to upload a file to OneDrive. Most of the times the function works fine, but sometimes the call returns with this error message:
System is busy with a previous background Upload...
I tried closing and restarting the application, but I am still getting the same error.
Any idea why it happens? How can I reinitialize the connection with OneDrive?
Here is the code to do the Upload:
public static async Task<string> BackgroundUploadFile<T>(
string skydriveFolderId,
T objectToSerialize,
string fileNameInSkyDrive,
BackgroundTransferPreferences backgroundTransferPreferences =
BackgroundTransferPreferences.AllowCellularAndBattery)
{
string fileId = string.Empty;
try
{
var storageFolder = await GetSharedTransfersFolder();
StorageFile isolatedstorageFile = await storageFolder.CreateFileAsync(
fileNameInSkyDrive,
CreationCollisionOption.ReplaceExisting);
using (var writer = new StreamWriter
(await isolatedstorageFile.OpenStreamForWriteAsync()))
{
// convert to string
var _String = Serialize(objectToSerialize);
await writer.WriteAsync(_String);
}
Client.BackgroundTransferPreferences = backgroundTransferPreferences;
LiveOperationResult liveOpResult = await Client.BackgroundUploadAsync(
skydriveFolderId,
new Uri("/shared/transfers/" + fileNameInSkyDrive, UriKind.Relative),
OverwriteOption.Overwrite);
fileId = (string)liveOpResult.Result["id"];
Debug.WriteLine("BackgroundUploadFile: " + fileNameInSkyDrive);
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Upload Error", MessageBoxButton.OK);
Debug.WriteLine("\nError - BackgroundUploadFile: " + e.Message);
}
return fileId;
}
Also, I have a "suspicion" that the OneDrive is locking the account because there are background tasks that are "stuck" and OneDrive does not release/free them?!
I couldn't make BackgroundUploadFile work as expected and solved it by using the attached upload function instead:
public static async Task<string> UploadFile<T>( string skydriveFolderId,
T objectToSerialize,
string fileNameInSkyDrive )
{
string fileID = string.Empty;
using ( var memoryStream = new MemoryStream() )
{
try
{
var serializer = new DataContractJsonSerializer( objectToSerialize.GetType() );
serializer.WriteObject( memoryStream, objectToSerialize );
memoryStream.Seek( 0, SeekOrigin.Begin );
LiveOperationResult liveOpResult = await Client.UploadAsync( skydriveFolderId, fileNameInSkyDrive, memoryStream, OverwriteOption.Overwrite );
fileID = (string)liveOpResult.Result [ "id" ];
Debug.WriteLine( "\nUploadFile: " + fileNameInSkyDrive );
}
catch ( Exception e )
{
MessageBox.Show( e.Message, "Upload Error", MessageBoxButton.OK );
Debug.WriteLine( "\nError - UploadFile: " + e.Message );
}
}
return fileID;
}
Thx