UWP 试图 运行 后台服务抛出异常

UWP trying to run background service throwing exception

我正在尝试 运行 UWP 应用程序中的后台服务。我首先检查应用程序是否具有后台权限。如果是,那么我正在为 运行ning.

注册服务

在我将 Visual Studio 和 Windows 10 SDK 更新到 Creators Update 版本之前,此代码工作正常。现在我不知道这个更新是否改变了注册后台服务的事情。

using System;
using Windows.ApplicationModel.Background;
using BackgroundService;
using SampleApp.Config;

namespace SampleApp.Background
{
    class BackgroundClass
    {
        LocalConfig LC = new LocalConfig();

        public async void RequestBackgroundAccess()
        {
            var result = await BackgroundExecutionManager.RequestAccessAsync();

            switch (result)
            {
                case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity:
                    break;
                case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity:
                    break;
                case BackgroundAccessStatus.Denied:
                    break;
                case BackgroundAccessStatus.Unspecified:
                    break;
            }
        }

        public async void RegisterBackgroundSync()
        {
            var trigger = new ApplicationTrigger();
            var condition = new SystemCondition(SystemConditionType.InternetAvailable);

            if (!LC.BackgroundSyncStatusGET())
            {
                var task = new BackgroundTaskBuilder
                {
                    Name = nameof(BackgroundSync),
                    CancelOnConditionLoss = true,
                    TaskEntryPoint = typeof(BackgroundSync).ToString(),
                };

                task.SetTrigger(trigger);
                task.AddCondition(condition);
                task.Register();

                LC.BackgroundSyncStatusSET(true);
            }

            await trigger.RequestAsync(); //EXCEPTION HAPPENS AT THIS LINE
        }

        public void RegisterBackgroundService(uint time)
        {
            var taskName = "BackgroundService";

            foreach (var unregisterTask in BackgroundTaskRegistration.AllTasks)
            {
                if (unregisterTask.Value.Name == taskName)
                {
                    unregisterTask.Value.Unregister(true);
                }
            }

            if(time != 0)
            {
                var trigger = new TimeTrigger(time, false);
                var condition = new SystemCondition(SystemConditionType.InternetAvailable);

                var task = new BackgroundTaskBuilder
                {
                    Name = nameof(BackgroundService),
                    CancelOnConditionLoss = true,
                    TaskEntryPoint = typeof(BackgroundService).ToString(),
                };

                task.SetTrigger(trigger);
                task.AddCondition(condition);
                task.Register();
            }
        }
    }
}

现在,在请求时我正在检查后台服务是否已注册,并保留重新注册的问题。我收到以下异常


System.Runtime.InteropServices.COMException 发生

HResult=0x80004005

消息=调用 COM 组件返回错误 HRESULT E_FAIL。

来源=Windows 堆栈跟踪:

在Windows.ApplicationModel.Background.ApplicationTrigger.RequestAsync()

在 SampleApp.Background.BackgroundClass.d__2.MoveNext()


请帮忙

在我的 Windows 10 隐私设置中遇到了同样的问题。

系统设置 => 隐私设置

在左侧菜单中选择 Background apps

检查以确保您的应用未被 运行 后台任务阻止。