我尝试将文件上传到 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()