/// <summary> /// Task canceled here means the app service client is gone /// </summary> private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) { AppServiceDeferral?.Complete(); AppServiceDeferral = null; Connection = null; AppServiceDisconnected?.Invoke(this, null); }
protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args) { base.OnBackgroundActivated(args); if (args.TaskInstance.TriggerDetails is AppServiceTriggerDetails details) { if (details.CallerPackageFamilyName == Package.Current.Id.FamilyName) { try { _appServiceDeferral = args.TaskInstance.GetDeferral(); args.TaskInstance.Canceled += (s, e) => { _appServiceDeferral?.Complete(); _appServiceDeferral = null; Connection = null; AppServiceDisconnected?.Invoke(this, null); }; Connection = details.AppServiceConnection; AppServiceConnected?.Invoke(this, args.TaskInstance.TriggerDetails as AppServiceTriggerDetails); _logger.Info("辅助进程服务连接成功"); } catch (Exception ex) { _logger.Error("辅助进程服务连接失败"); _logger.Error(ex); } } } else { _logger.Error("辅助进程服务连接失败"); _logger.Error(args.TaskInstance.TriggerDetails.GetType()); } }
private void Connection_OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) { if (AppServiceDeferral != null) { AppServiceDeferral.Complete(); } AppServiceDisconnected?.Invoke(this, null); }