在我看来,我的后台任务没有工作/触发。有什么办法可以调试它们,或者用集成测试来测试它。
这是一个示例后台任务:
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
和以下入口点定义了一个新的System Event
:MyNamespace.MyBackgroundTask
注意: 后台任务与app(后端/前端分离)
在不同的程序集中