如何解决 BackgroundUploadAsync 错误?
How to solve BackgroundUploadAsync error?
我正在使用 BackgroundUploadAsync
API 将文件上传到 OneDrive。大多数时候该函数工作正常,但有时调用 returns 并出现此错误消息:
System is busy with a previous background Upload...
我尝试关闭并重新启动应用程序,但我仍然遇到同样的错误。
知道为什么会这样吗?如何重新初始化与 OneDrive 的连接?
这是执行上传的代码:
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;
}
此外,我 "suspicion" OneDrive 正在锁定帐户,因为有后台任务 "stuck" 而 OneDrive 没有 release/free 它们?!
你有机会在其他任何地方打电话给 BackgroundUploadAsync
吗?尝试用 SemaphoreSlim
任何地方 保护它,你可能会使用它:
// 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();
}
// ...
}
我无法让 BackgroundUploadFile 按预期工作,而是使用附加的上传功能解决了这个问题:
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;
}
感谢
我正在使用 BackgroundUploadAsync
API 将文件上传到 OneDrive。大多数时候该函数工作正常,但有时调用 returns 并出现此错误消息:
System is busy with a previous background Upload...
我尝试关闭并重新启动应用程序,但我仍然遇到同样的错误。
知道为什么会这样吗?如何重新初始化与 OneDrive 的连接?
这是执行上传的代码:
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;
}
此外,我 "suspicion" OneDrive 正在锁定帐户,因为有后台任务 "stuck" 而 OneDrive 没有 release/free 它们?!
你有机会在其他任何地方打电话给 BackgroundUploadAsync
吗?尝试用 SemaphoreSlim
任何地方 保护它,你可能会使用它:
// 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();
}
// ...
}
我无法让 BackgroundUploadFile 按预期工作,而是使用附加的上传功能解决了这个问题:
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;
}
感谢