1

我尝试将文件上传到 OneDrive。我已成功连接并验证 OneDrive,但尝试上传时出现错误:Object reference not set to an instance of an object.

代码是:

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();

BackgroundUploadAysnc方法抛出异常。oneDriveFolderId设置并存在于 OneDrive 中。DatabaseBackupname 是 OneDrive 中不存在的正确新文件名。我在调试器中检查了流,这个对象不是空的并且有一个大小。

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()

我将上传代码更改为:

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

并且该CreateBackgroundUploadAsync方法引发以下异常:

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()
4

3 回答 3

0

我在这个确切的问题上浪费了几个小时。对我来说,它发生在我正在迁移到 Windows Phone 8.1 Silverlight 的 Windows Phone 8.0 应用程序上。最终为我解决问题的是更新到适用于 Windows Phone 的最新 Microsoft Advertising SDK。我从版本 6.2.960.0 升级到 8.1.50112.0。在那之后我重建后,我上传到 OneDrive 的文件就开始正常工作了。完全不确定根本原因是什么,但我想我会在这里分享它,以防它帮助任何人。

需要明确的是,我的项目文件来自:

<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" />

对此:

<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" />
于 2015-03-14T16:13:35.220 回答
0

这并不是错误消息的真正解决方案,但我将 Live.SDK 引用更改为 Windows Phone dll,现在我可以使用 BackgroundUploadAsync(path, uri, overwrite) 方法并且它可以工作。

我还必须更改 Auth 方法。

于 2014-09-14T20:38:20.687 回答
0

我不得不将我的应用程序“与商店”“关联”起来。

在 Visual Studio 的解决方案资源管理器中右键单击项目行。

左键单击 Store -> 将 App 与 Store 关联

我必须注册为 Microsoft 开发人员(并为此付费),但是一旦我这样做了,我对 BackgroundUploadAsync 的调用就会返回合理的值。

因为它把我带到了我最终会去的方向,我想,这到底是怎么回事。

于 2015-06-02T18:39:39.680 回答