1
votes

I am trying to run background service in UWP application. I am first checking if application has background permission. If yes then I am registering the service for running.

This code was working fine until I updated Visual Studio along with Windows 10 SDK to Creators Update version. Now I can't figure out if this update changes things for registering background service.

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();
            }
        }
    }
}

Now while requesting I am checking if background service is registered keeping issues for re-registration. I am getting following exception


System.Runtime.InteropServices.COMException occurred

HResult=0x80004005

Message=Error HRESULT E_FAIL has been returned from a call to a COM component.

Source=Windows   StackTrace:   

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

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


Please Help

1
The code you've posted seems right. The problem here may not here. Have you tried with Background task sample? Does it have the same issue? Also you can try with your code on other devices. If you still have the same problem, I'd suggest you share a minimal reproducible example so that we can reproduce your issue. - Jay Zuo

1 Answers

1
votes

Had this same problem, was in my Windows 10 Privacy Settings.

System Settings => Privacy Settings

In the left-hand menu choose Background apps.

Check to make sure your app hasn't been blocked from running background tasks.