protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args) { base.OnBackgroundActivated(args); IBackgroundTaskInstance taskInstance = args.TaskInstance; var deferral = taskInstance.GetDeferral(); AppServiceTriggerDetails appService = taskInstance.TriggerDetails as AppServiceTriggerDetails; Debug.WriteLine($"appService.CallerPackageFamilyName: {appService.CallerPackageFamilyName}"); // blank if Excel if ((string.IsNullOrEmpty(appService.CallerPackageFamilyName)) && (_excelConnection == null)) { if (_excelConnection != null) { ExcelResetConnection(); } _excelConnection = appService.AppServiceConnection; _excelConnection.RequestReceived += ExcelOnAppServiceRequestReceived; _excelConnection.ServiceClosed += ExcelConnectionServiceConnection_ServiceClosed; _excelAppServiceDeferral = deferral; taskInstance.Canceled += ExcelOnAppServicesCanceled; Debug.WriteLine($"Connecting excel Service {_excelConnection.GetHashCode()} {"Excel"}"); } else { if (_dataConnection != null) { DataConnectionResetConnection(); } _dataConnection = appService.AppServiceConnection; _dataConnection.RequestReceived += DataServiceRequestReceived; _dataConnection.ServiceClosed += DataServiceConnection_ServiceClosed; _dataConnectionDeferral = deferral; taskInstance.Canceled += DataOnAppServicesCanceled; Debug.WriteLine($"Connecting Data Service {_dataConnection.GetHashCode()} {appService.CallerPackageFamilyName}"); } }
private async void ExcelOnAppServiceRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) { var messageDeferral = args.GetDeferral(); var request = args.Request; var m = request.Message; object command; m.TryGetValue("Command", out command); try { if (command as string == "Connect") { object role; m.TryGetValue("Role", out role); if ((role as string) == "DataStreamer") { _excelConnection = sender; Debug.WriteLine($"Connecting Excel: {sender.GetHashCode()}"); } var response = new ValueSet(); response.Add("Response", "OK"); await request.SendResponseAsync(response); SendStatusAsync(); } } catch (Exception e) { Debug.WriteLine($"Exception while sending the response : {e.Message}"); } finally { // Complete the deferral so that the platform knows that we're done responding to the app service call. // Note for error handling: this must be called even if SendResponseAsync() throws an exception. messageDeferral.Complete(); } }