public async void Terminate()
 {
     await IPHelperUtils.TerminateIPHelper(Connection);
 }
        protected override async void OnBackgroundActivated(BackgroundActivatedEventArgs args)
        {
            base.OnBackgroundActivated(args);
            if (!(args.TaskInstance.TriggerDetails is AppServiceTriggerDetails))
            {
                return;
            }
            AppServiceConnection connection = ((AppServiceTriggerDetails)args.TaskInstance.TriggerDetails).AppServiceConnection;
            var appServiceDeferral          = args.TaskInstance.GetDeferral();

            switch (connection.AppServiceName)
            {
            case "IPHelperService":
                args.TaskInstance.Canceled += (sender, reason) =>
                {
                    try
                    {
                        appServiceDeferral.Complete();
                    }
                    catch (Exception)
                    {     // ignored
                    }
                };
                if (Contexts.Count <= 0)
                {
                    await IPHelperUtils.TerminateIPHelper(connection);

                    appServiceDeferral.Complete();
                    return;
                }
                var context = Contexts.First();
                context.Connection          = connection;
                context.Deferral            = appServiceDeferral;
                args.TaskInstance.Canceled += (sender, reason) => context.DoFinal();
                connection.RequestReceived += IPHelperUtils.ConnectionOnRequestReceivedAsync;
                connection.ServiceClosed   += IPHelperUtils.OnServiceClosed;
                break;

            case "IPDiscoverService":
                args.TaskInstance.Canceled += (sender, reason) =>
                {
                    try
                    {
                        appServiceDeferral.Complete();
                    }
                    catch (Exception)
                    {     // ignored
                    }
                };
                IPHelperUtils.GetIPFromMAC((result, last) =>
                {
                    if (last)
                    {
                        appServiceDeferral.Complete();
                    }
                    if (result == null)
                    {
                        return(true);
                    }
                    connection.SendMessageAsync(new ValueSet {
                        ["op"] = "result", ["addr"] = result
                    }).GetAwaiter()
                    .GetResult();
                    return(true);
                });
                connection.ServiceClosed += (sender, a) =>
                {
                    try
                    {
                        appServiceDeferral.Complete();
                    }
                    catch (Exception)
                    {
                        //ignored
                    }
                };
                break;

            default:
                appServiceDeferral.Complete();
                break;
            }
        }