我尝试将文件上传到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()
答案 0 :(得分:0)
这不是错误消息的真正解决方案,但我将Live.SDK引用更改为Windows Phone dll,现在我可以使用BackgroundUploadAsync(path,uri,overwrite)方法,它可以正常工作。
我还必须更改Auth方法。
答案 1 :(得分:0)
我在这个问题上浪费了几个小时。对我来说,它发生在我正在迁移到Windows Phone 8.1 Silverlight的Windows Phone 8.0应用程序上。最终为我解决的问题是更新到最新的Microsoft Advertising SDK for Windows Phone。我从版本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" />
答案 2 :(得分:0)
我必须&#34;关联&#34;我的应用程序&#34;与商店。&#34;
在Visual Studio的解决方案资源管理器中右键单击项目行。
左键单击Store - &gt;将应用程序与商店关联
我必须以Microsoft开发人员的身份注册(并为此付费),但是一旦我这样做,我对BackgroundUploadAsync的调用返回了合理的值。
因为无论如何我带着我最终会去的方向,我想,哎呀。