HttpClient.GetAsync 在具有锁定屏幕访问权限以及 TimeTrigger 或 MaintenanceTrigger 的后台任务中失败

HttpClient.GetAsync fails in background task with lock screen access and both TimeTrigger or MaintenanceTrigger(HttpClient.GetAsync 在具有锁定屏幕访问权限以及 TimeTrigger 或 MaintenanceTrigger 的后台任务中失败)

本文介绍了HttpClient.GetAsync 在具有锁定屏幕访问权限以及 TimeTrigger 或 MaintenanceTrigger 的后台任务中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Windows 8 Metro 应用程序的后台任务中运行时,Client.GetAsync 对我来说似乎失败了.

Client.GetAsync appears to fail for me when running in a background task in my Windows 8 Metro app.

我尝试同时使用 TimeTrigger 和 MaintenanceTrigger.看来也不例外.调试它时,它只会在该行退出(如果我一直按 step over),即使还有更多的方法可以执行以及围绕它的 try-catch.

I tried using both a TimeTrigger and a MaintenanceTrigger. It appears that there is no exception. When debugging it, it just exits at that line (if I keep pressing step over), even though there is way more to execute as well as a try-catch around it.

这让我相信 Windows 正在取消我的任务.所以我听了被取消的事件,没有运气.

This leads me to believe that Windows is cancelling my task. So I listened to the cancelled event with no luck.

我已将 Internet 访问声明为一项功能,并且该任务已正确注册.此外,我的应用具有完整的锁屏访问权限.

I have declared Internet access as a capability, and the task has been properly registered. Furthermore, my app has full lock screen access.

我做错了什么?

这是我目前用来注册后台任务的代码.

Here's the code I use to register the background task currently.

if (await ObtainLockScreenAccess())
{
    const string name = "Task";

    var updaterTasks = BackgroundTaskRegistration.AllTasks.Where(t => t.Value.Name == name);
    if (!updaterTasks.Any())
    {

        var builder = new BackgroundTaskBuilder();
        builder.Name = name;
        builder.TaskEntryPoint = "Stackstabilizer.BackgroundTasks.UpdateBackgroundTask";

        var trigger = new MaintenanceTrigger(15, false);
        var condition = new SystemCondition(SystemConditionType.InternetAvailable);

        builder.AddCondition(condition);
        builder.SetTrigger(trigger);

        var task = builder.Register();

        Debug.WriteLine("Task has been registered");

    }
}

编辑这是后台任务签名:

namespace Stackstabilizer.BackgroundTasks
{
    // You must use a sealed class, and make sure the output is a WINMD. 
    public sealed class UpdateBackgroundTask : IBackgroundTask
    {
        public async void Run(IBackgroundTaskInstance taskInstance)
        {

            var deferral = taskInstance.GetDeferral();

            try { ... } finally { deferral.Complete(); }

        }
     }

}

这里有一些关于我的声明的细节.

And here's some details on my declarations.

推荐答案

我之前也遇到过类似的问题,我解决的方法是在异步任务中再延期一次.我认为这个想法是,每当您有异步任务时,您都需要延迟.(我不确定这是否是最佳做法,但嘿,它有效)

I had a similar problem to you before, and the way I solved it was to have another deferral in the async task. I think the idea is that, whenever you have an async task, you need a deferral. (I'm not sure if it's best practice, but hey, it works)

代码:

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        updateAllTiles(taskInstance);
    }


    public async void updateAllTiles(IBackgroundTaskInstance taskInstance)
    {
        // Need to use a deferral to run async tasks in the background task
        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

        dosomething(taskInstance);

        deferral.Complete();
    }

    private async void dosomething(IBackgroundTaskInstance taskInstance)
    {
        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

        HttpClient client = new HttpClient();
        HttpResponseMessage resp = await client.GetAsync(new Uri(url));

        updateTile(resp);

        deferral.Complete();
    }

这篇关于HttpClient.GetAsync 在具有锁定屏幕访问权限以及 TimeTrigger 或 MaintenanceTrigger 的后台任务中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:HttpClient.GetAsync 在具有锁定屏幕访问权限以及 TimeTrigger 或 MaintenanceTrigger 的后台任务中失败

基础教程推荐