How to create App Service communication in Windows Universal Apps.

By | August 6, 2015

App Service is a way to communicate between universal apps where one app can communicate with the other app’s background task. The user don’t even know that the app is using some other app’s background task to complete its operation. It is similar to using a web service where the user send the data to the service it process it and send back the response data back to the app. But here we don’t use any http protocol or any web service instead we use a background task which accept the data from another app.

App Comm_image

The actual usage of App Service comes when one app has a background tasks which is very generic, then there is no use for all others apps to have the same code in their background task. Instead we may reuse the same code which already exist.

In this sample we are going to create a simple app which is used for add the data and return it.

Create a Universal Windows app and display two text box to get the data and a button to calculate it.
On button click add the following code.

AppServiceConnection appServiceCon = new AppServiceConnection();

appServiceCon.AppServiceName = "appservicesdemo";

appServiceCon.PackageFamilyName = "PackageFamilyName";

AppServiceConnectionStatus connectionStatus = await appServiceCon.OpenAsync();

if (connectionStatus == AppServiceConnectionStatus.Success)

var message = new ValueSet();

message.Add("Command", "AddData");

message.Add("Value1", Int32.Parse(TextBox1.Text));

message.Add("Value2", Int32.Parse(TextBox2.Text));

AppServiceResponse response = await appServiceCon.SendMessageAsync(message);

if (response.Status == AppServiceResponseStatus.Success)


int sum = (int)response.Message["Result"];

new MessageDialog("Added Result=" + sum).ShowAsync();





new MessageDialog("Connection Failed ").ShowAsync();


Where AppServiceConnection is the class which helps the app to connect to another apps background task.

The method appServiceCon.OpenAsync() tries to open the background task. If this succeeded then the user can send the data to the background task.

SendMessageAsync() is used to send the message to the background task of another app and expects a reply form the background task. Once the reply is received then it can be displayed in the UI.

We have Written the client side of the app, lets see how to write the Background task.

Create a class which implements IBackgroundTask interface and add this class as a reference to another universal apps.

namespace AppServiceDemo


public sealed class AppServiceBGTask : IBackgroundTask


private static BackgroundTaskDeferral serviceDeferral;

public void Run(IBackgroundTaskInstance taskInstance)


taskInstance.Canceled += TaskInstance_Canceled;

serviceDeferral = taskInstance.GetDeferral();

var appService = taskInstance.TriggerDetails as AppServiceTriggerDetails;

if (appService.Name == "appservicesdemo")


appService.AppServiceConnection.RequestReceived += RequestReceived;



So when this Background task gets triggered by the some other app it check for the condition and if the condition gets satisfies then the app subscribe for the receive request event.

Once the request is received by this task. Then the following method gets executed and returns the result to the called App.

private async void RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
var messageDeferral = args.GetDeferral();
var message = args.Request.Message;
int value1 = (int)message["Value1"];
int value2 = (int)message["Value2"];
result = value1 + value2;
var returnMessage = new ValueSet();
returnMessage.Add("Result", result);
var responseStatus = await args.Request.SendResponseAsync(returnMessage);

One more important things the app which is going to have the background task should have the following entry kin the AppManifest file. The AppServiceName in the code and the Name of the AppService in the manifest should match else this won’t work.

<uap:Extension Category="windows.appService" EntryPoint=" AppServiceDemo. AppServiceBGTask ">
<uap:AppService Name=" appservicesdemo "/>

Hope this post might have helped in creating an App Service and share the background task between apps.