7

在我看来,我的后台任务没有工作/触发。有什么方法可以调试它们,或者通过集成测试对其进行测试。

这是一个示例后台任务:


public sealed class MyBackgroundTask : IBackgroundTask
{
 private ITileService _tileService;

 //Tried a parameter-less constructor in case IoC doesn't work
 public MyBackgroundTask() : this(new TileService)  {}

 public MyBackgroundTask(ITileService tileService)
 {
     _tileService = tileservice;
 }

        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            Debug.WriteLine("MyBackgroundTask is running " + taskInstance.Task.Name );
            taskInstance.Canceled += TaskInstanceOnCanceled;
            if (!_cancelRequest && SomeOtherCondition)
            {
                var deferral = taskInstance.GetDeferral(); 
                await _tileService.UpdateLiveTile(null);
                deferral.Complete();
            }
        }
}

后台任务注册:(此代码运行,用调试器检查)


var backgroundTaskBuilder = new BackgroundTaskBuilder
                                                {
                                                    TaskEntryPoint =
                                                        "MyNamespace.MyBackgroundTask",
                                                    Name = "MyBackgroundTask"
                                                };

                backgroundTaskBuilder.SetTrigger(new MaintenanceTrigger(15, false));
                backgroundTaskBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
                backgroundTaskBuilder.Register();

在应用程序清单中,我定义了一个新BackgroundTask的 aSystem Event和以下入口点:MyNamespace.MyBackgroundTask

注意:后台任务与app在不同的程序集中(后端/前端分离)

4

0 回答 0