App Service communication together with Windows Universal App, WPF and Winforms Application

App Service communication together with Windows Universal App, WPF and Winforms Application

App Service is a way to communicate between apps where one app can communicate with the other app’s background task.We have already learned about How to create App Service communication in Windows Universal Apps.  In this post we will see how to access the Background task created for a Windows Universal App by the WPF/Winforms application.

App-Comm_image1

In this sample I am just creating the WPF client to access the App Service which we have already created in one of the previous post. You can get to know about it from here.

Step 1: Add the following dll’s and winmd to the WPF  Application.

System.Runtime.dll

System.Runtime.WindowsRuntime.dll

Windows.winmd

You can add these dll’s and winmd  from the following location.

C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Runtime.dll

C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Runtime.WindowsRuntime.dll

C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd

winmd

dll

 

Now your WPF Application has the above mentioned dll’s and winmd files. Now you can directly access the windows 10 API to call the App Services.  In this sample we are going to create a simple app which is used for add the data and return it.

Step 2:  Create a WPF application 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 = "2f7cc76d-4156-4f03-94a8-8aa57ec7450a_b7t4pw2z5kc4y";
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"];
    }
}

That’s it the WPF application now access the Universal Windows App’s Background Task and perform its operation. If the universal app have not created yet. Please have a look at this post.

I Hope this post helps in accessing the Universal App’s  App Service in WPF/Winforms Application.

Arun Kumar

Arun kumar Surya Prakash, is a  Developer  Consultant. He  is a Microsoft Certified Solution  Developer  for Store App Development, and a Azure Solution Developer. His core skills is on developing Windows Store App and Cross Platform App development. You can follow him @arunnov04