1

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?!

eitan barazani
  • 1,123
  • 3
  • 18
  • 34

2 Answers2

1

Is there a chance you call BackgroundUploadAsync anywhere else? Try protecting it with SemaphoreSlim anywhere you might be using it:

// class member
SemaphoreSlim  _semaphore = new SemaphoreSlim(1); 

public static async Task<string> BackgroundUploadFile<T>(..)
{
    // ...

    LiveOperationResult liveOpResult;
    await _semaphore.WaitAsync();
    try
    {
        liveOpResult = await Client.BackgroundUploadAsync(
            skydriveFolderId,
            new Uri("/shared/transfers/" + fileNameInSkyDrive, UriKind.Relative),
            OverwriteOption.Overwrite);
    }
    finally
    {
        _semaphore.Release();
    }

    // ...
}
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • Thx. No I don't call it anywhere. I wonder if I need to clean up maybe after calling it?! The code is more than a year old, and I noticed the problem in the last month. I wonder if Live 5.6 or something else in Microsoft changed?! – eitan barazani Feb 25 '15 at 22:54
  • @eitanbarazani, they could. Try creating/disposing of the `Client` every time you need it. – noseratio Feb 25 '15 at 23:56
  • 1
    I will try that. Thanks for the suggestion. BTW, one more observation, when the account/OneDrive "get locked", shutting the application or turning the phone off WILL NOT unlock the account/OneDrive. It is a strange behavior. – eitan barazani Feb 26 '15 at 01:41
0

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

eitan barazani
  • 1,123
  • 3
  • 18
  • 34