Before reading this article, please go through the following articles:
This article is in continuation of part five of the series of articles and explains how communication happens between two apps, with examples.
In the below sample application, one app sends the information to AppService and the another app reads the information from the AppService. The following diagram explains about the steps.
Two apps are having the same procedure except the command. The first one sends the set (write) while the second app gets (read) request. For better AppService communication, we will create a common class library which both the apps will share.
Note: Creation of AppService communication has been explained in Part 5.
Create a Class Library
- Create a Class Library Project.
- Class Library Implementation.
- public static class AppServiceManager
- {
- private static AppServiceConnection _appService;
- private static AppServiceConnectionStatus _appStatus;
-
- public static async Task<bool> OpenAsync(string serviceName,string packageName)
- {
- _appService = new AppServiceConnection
- {
- AppServiceName = serviceName,
- PackageFamilyName = packageName
- };
-
- _appStatus = await _appService.OpenAsync();
-
- return _appStatus == AppServiceConnectionStatus.Success;
- }
-
- public static async Task<string> SendMessageAsync(string command,string message)
- {
- if(_appStatus != AppServiceConnectionStatus.Success)
- throw new Exception("AppService has failed!!!");
-
- var responseUpdate = new ValueSet();
- responseUpdate.Add("Command", command);
- responseUpdate.Add("serverMsg", message);
-
- var response = await _appService.SendMessageAsync(responseUpdate);
- var result = response.Message["Result"] as object;
- return (string)result;
- }
-
- }
Build the project
Main Application Implementation with AppService Registration.
- Create a write application ( explained in Part 5) with AppService Registration.
- Add Class Library reference to communicate the AppService.
- Implement the Connection and Command.
- private const string AppServiceName = "com.microsoft.BgAppService";
- private const string PackageFamilyName = "69533c06-5ce5-4f1a-93fe-151ca737537a_kz0gnaa3h8516";
-
-
-
- private async void OpenService()
- {
- await AppServiceManager.OpenAsync(AppServiceName, PackageFamilyName);
- }
-
- public async void CreateService(string sendMsg)
- {
- var textMsg = await AppServiceManager.SendMessageAsync("set", sendMsg);
- TxtSendStatus.Text = textMsg;
- }
- Textbox is entering event, Send the message to Service
- private void TxtStatus_OnKeyDown(object sender, KeyRoutedEventArgs e)
- {
- if (e.Key != VirtualKey.Enter) return;
- TxtSendStatus.Text = "Send Msg";
- var sendMsg = TxtStatus.Text;
- CreateService(sendMsg);
- }
- Service code Command Handling
- private async void AppServiceRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
- {
- var serviceMsg = args.GetDeferral();
-
- var storeData = args.Request.Message;
- string command = storeData["Command"] as string;
-
-
- ValueSet returnData = new ValueSet();
-
- if (command == "set")
- {
- string inventoryIndex = storeData["serverMsg"] as string;
- ApplicationData.Current.RoamingSettings.Values["set"] = inventoryIndex;
- returnData.Add("Result", "OK");
- await args.Request.SendResponseAsync(returnData);
- }
- else if (command == "get")
- {
- string inventoryIndex = (string) ApplicationData.Current.RoamingSettings.Values["set"];
- returnData.Add("Result", inventoryIndex);
- await args.Request.SendResponseAsync(returnData);
- }
-
- serviceMsg.Complete();
- }
Another Client reads from the Service.
- Create a Blank App (Universal Windows).
- RequestService gets the message (Add reference ServiceManager dll).
- public async void RequestService()
- {
- try
- {
- await AppServiceManager.OpenAsync(AppServiceName, PackageFamilyName);
-
- RecvBlock.Text = await AppServiceManager.SendMessageAsync("get", "");
- }
- catch (Exception exception)
- {
-
- }
- }
- Implement DispatcherTimer and read the information each second, from the AppService.
- var dispatcherTimer = new DispatcherTimer();
- dispatcherTimer.Tick += DispatcherTimer_Tick;
- dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);
- dispatcherTimer.Start();
-
-
- private async void DispatcherTimer_Tick(object sender, object e)
- {
- await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, RequestService);
- }
Build & Deploy the Project
Output
Run the two apps at the same time. One is for sending while another one is for receiving the information.
Overview this sample
- BGAppService: Running the AppService.
- ServiceManager: Class Library for communication to the BGAppService
- MainApplication: Registers BGAppService and sends the information to the BGAppService using the ServiceManager Library.
- ClientUI: Using the ServiceManager Library reads the information from the BGAppService, using DispatchTimer.