1

I tried to upload a file to OneDrive. I'm successfully connecting and authenticating to OneDrive but when attempting to upload, I get the error: Object reference not set to an instance of an object.

The code is:

IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
// Release all resources from DB 
App.AppViewModel.DisposeCurrentDB();

IsolatedStorageFileStream toUploadStream = iso.OpenFile(AppResources.DatabaseName + ".sdf", FileMode.Open);

LiveConnectClient liveClient = new LiveConnectClient(oneDriveAuthClient.Session);

try
{
    LiveOperationResult uploadResult = await liveClient.BackgroundUploadAsync(
        oneDriveFolderId, 
        DatabaseBackupname, 
        toUploadStream.AsInputStream(),
        OverwriteOption.Overwrite);

    dynamic result = uploadResult.Result;
    MessageBox.Show("Upload successful. Uploaded to " + result.source);
}
catch (LiveConnectException ex)
{
    MessageBox.Show("Error uploading backup: " + ex.Message);
}

App.AppViewModel.RefreshCurrentDB();

The BackgroundUploadAysnc method throws the exception. The oneDriveFolderId is set and exists in OneDrive. DatabaseBackupname is a correct new filename which doesn't exist in OneDrive. I checked the stream in debugger and this object was not empty and had a size.

Stacktrace
   at Microsoft.Live.Operations.TailoredUploadOperation.<OnGetUploadLinkCompleted>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__4(Object state)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

I changed the upload code to this:

LiveUploadOperation operation = await liveClient.CreateBackgroundUploadAsync(
    oneDriveFolderId, 
    DatabaseBackupname, 
    toUploadStream, 
    OverwriteOption.DoNotOverwrite);
await operation.StartAsync();

and the CreateBackgroundUploadAsync method throws the following exception:

Message = "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"
   at Windows.Networking.BackgroundTransfer.BackgroundUploader.CreateUploadFromStreamAsync(Uri uri, IInputStream sourceStream)
   at Microsoft.Live.Operations.CreateBackgroundUploadOperation.<OnGetUploadLinkCompleted>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__4(Object state)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
GusP
  • 2,454
  • 2
  • 23
  • 32
Kn0rK3
  • 73
  • 7

3 Answers3

0

It is not really a solution for the error message, but I changed the Live.SDK reference to the Windows Phone dll and now I can use the BackgroundUploadAsync(path, uri, overwrite) method and it works.

I also had to change the Auth method.

Kn0rK3
  • 73
  • 7
0

I wasted a few hours on this exact problem. For me it was happening on a Windows Phone 8.0 app that I was migrating to Windows Phone 8.1 Silverlight. What finally resolved things for me was updating to the latest Microsoft Advertising SDK for Windows Phone. I went from version 6.2.960.0 to 8.1.50112.0. As soon as I rebuilt after that, my uploads to OneDrive started to work perfectly. Not at all sure what the root cause was but figured I'd share it here in case it helps anyone.

To be clear, my project file went from this:

<Reference Include="Microsoft.Advertising.Mobile, Version=6.2.960.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="Microsoft.Advertising.Mobile.UI, Version=6.2.960.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />

to this:

<Reference Include="Microsoft.Advertising.Mobile, Version=8.1.50112.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="Microsoft.Advertising.Mobile.Common, Version=8.1.50112.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="Microsoft.Advertising.Mobile.UI, Version=8.1.50112.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
GusP
  • 2,454
  • 2
  • 23
  • 32
0

I had to "associate" my app "with the Store."

Right-click on the project line in the Solution Explorer in Visual Studio.

Left-click on Store -> Associate App with the Store

I had to sign up as a Microsoft developer (and pay for it), but once I did that, my calls to BackgroundUploadAsync returned reasonable values.

Since it was taking me in the direction I would eventually be going anyway, I figured, What the Heck.

LionelGoulet
  • 557
  • 2
  • 13