从Live SDK调用BackgroundDownloadAsync或BackgroundUploadAsync后的未知/空后台任务

时间:2014-08-19 09:14:32

标签: c# windows-phone-8.1 onedrive live-sdk

知道为什么每当调用BackgrouDownloadAsync或BackgroundUploadAsync时,Lifecycle Events下都会列出一个“空”任务?

我还不能发布图片。请参阅http://social.msdn.microsoft.com/Forums/en-US/39944e7d-feb2-4e06-b980-6ff41588ec50/unknown-empty-background-task?forum=wpdevelop

中的原始帖子

1 个答案:

答案 0 :(得分:1)

这是我用来替换BackgroundDownloadAsync和BackgroundUploadAsync所做的,省略了任何try-catch和错误检查代码。这些调用不会导致"生命周期事件"中的空条目落下。不确定是否是VS 2013中的问题。

下载(这很麻烦,必须有另一种方式)

LiveConnectClient connectClient = new LiveConnectClient(this.Session);
LiveOperationResult _opResult = await connectClient.GetAsync(FileID + "/content");
dynamic _result = _opResult.Result;

CancellationTokenSource cts = new CancellationTokenSource();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(_result.location as string, UriKind.Absolute));
HttpResponseMessage response = await httpClient.SendRequestAsync(request, HttpCompletionOption.ResponseHeadersRead).AsTask(cts.Token);

using (var _stream = (await OutputFile.OpenStreamForWriteAsync()).AsOutputStream())
{
    await response.Content.WriteToStreamAsync(_stream).AsTask(cts.Token);
    await _stream.FlushAsync();
}

上传

LiveConnectClient connectClient = new LiveConnectClient(this.Session);
using (Stream stream = await InputFile.OpenStreamForReadAsync())
{
    using (StreamReader reader = new StreamReader(stream))
    {
        LiveOperationResult _opResult = await connectClient.PutAsync(Awesome2FolderID + "/files/" + OneDriveFilename, reader.ReadToEnd());
    }
}