App Services

Client

AppServiceConnection connectio = new AppServiceConnection();
connection.AppServiceName = "microsoftDX-appservicesdemo";   // Manifest of the App which host the App Service, must only be unique in the app
connection.PackageFamilyName = "24919ArunjeetSingh.InstapaperIt";   // From Store, after registered the App
AppServiceConnectionStatus connectionStatus = await connection.OpenAsync();:

if(connectionStatus == AppServiceConnectionStatus.Success)
{
  var message = new ValueSet();
  message.Add("Command", "CalcSum");
  message.Add("Value1", Int.Parse(Value1.Text));
  message.Add("Value2", Int.Parse(Value2.Text));
  AppServiceResponse response = await connection.SendMessageAsync(message);

  if(response.Status == AppServiceResponseStatus.Success)
  {
    int sum = (int)response.Message["Result"];
    new MessageDialog("Result=" + sum).ShowAsync();
  }
}
else
{
  // Drive the user to the store to install the app that provides the app service
}

Service

AppService

namespace AppServicesDemoTask
{
  public sealed class AppServiceTask : 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 == "microsoftDX-appservicesdemo")
  {
    appService.AppServiceConnection.RequestReceived += RequestReceived;
  }

Request Received

private async void RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
  var message = args.Request.Message;
  string command = message["Command"] as string;
  switch (command)
  {
    case "DoIt":
      var messageDeferral = args.GetDeferral();
      int value1 = (int)message["Value1"];
      // ... calc
      var returnMessage = new ValueSet();
      returnMessage.Add("Result", result);
      var responseStatus= await args.Request.SendResponseAsync(returnMessage);
      messageDeferral.Complete();
    break;

    case "Quit":
      _serviceDeferral.Complete();
    break;
  }
}

Declaring App Service

Manifest

<?xml ...
<Package ...
 <Applications>
  <Application Id="App" ...>
   <Extensions>
    <uap:Extension Category="windows.appService" EntryPoint="AppServicesDemoTask.AppServiceTask">
    <uap:AppService Name="microsftDX-appServicesdemo" />   // must be unique in the app.
    <uap:Extension>
   </Extensions>
 </Applications>

Two-way Communication

Client

AppServiceConnectionStatus connectionStatus = await connection.OpenAsync();

if(connectionSttaus == AppServiceConnectionStatus.Success)
{
  connection.RequestReceived += OnRequestReceived;
}

 Debugging Tips

var currentPackage = Windows.ApplicationModel.Package.Current;
var packageFamilyName = currentPackage.Id.FamilyName;

 Debugging

  1. Set breakpoints in app service
  2. Check „Do not launch, but debug my code when it starts“ in project properties
  3. Launch app service foreground app in debugger
  4. Run client app  to connect to app servcie
  5. Debugger attaches and breaks on your breakpoint