Overall the implementation has described below
We will see in details, how to implement this concept, let us take a simple example: the main application has been providing some maths functionality (like addition and subtraction) to outside world, other application can interact with this application to use this features if they required.
namespace BGAppService
{
public sealed class BgAppService : IBackgroundTask
public void Run(IBackgroundTaskInstance taskInstance)
}
AppServiceTriggerDetails: Implement app services enable app-to-app communication by allowing providing as services
ValueSet: All the data communication via ValueSet class
Request Received: Whenever client application request, Request received event received the message and process the request and send to back the client to the response.
01.
public
void
Run(IBackgroundTaskInstance taskInstance)
02.
03.
_taskDeferral = taskInstance.GetDeferral();
04.
taskInstance.Canceled += TaskInstance_Canceled;
05.
AppServiceTriggerDetails taskAppService = taskInstance.TriggerDetails
as
AppServiceTriggerDetails;
06.
if
(taskAppService !=
null
) _serviceConnection = taskAppService.AppServiceConnection;
07.
(_serviceConnection !=
) _serviceConnection.RequestReceived += _serviceConnection_RequestReceived;
08.
09.
10.
private
async
c(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
11.
12.
var serviceMsg = args.GetDeferral();
13.
var request = args.Request.Message;
14.
string
command = request[
"Command"
]
;
15.
arg1 = request[
"arg1"
16.
arg2 = request[
"arg2"
17.
18.
int
result = 0;
19.
20.
(
.Compare(command,
"add"
,
true
) == 0)
21.
22.
result = Convert.ToInt16(arg1) + Convert.ToInt16(arg2);
23.
24.
else
"sub"
25.
26.
result = Convert.ToInt16(arg1) - Convert.ToInt16(arg2);
27.
28.
29.
ValueSet sendResult =
new
ValueSet();
30.
sendResult.Add(
"Result"
, result.ToString());
31.
await args.Request.SendResponseAsync(sendResult);
32.
33.
serviceMsg.Complete();
34.
1. AppServiceConnection object: Connection to the end point for an app service, to create an AppServiceObject. server app "AppserviceName and PackageFamilyName" property have required creating a connection object.
AppServiceName: Its declare in the main application Package.appxmanifest file PackageFamilyName: main application packageFamilyName ( Note: To get package family name use Windows.ApplicationModel.Package.Current.Id.FamilyName property)
var appConnection =
AppServiceConnection
AppServiceName =
"com.microsoft.appservice"
PackageFamilyName =
"AppService.Extension.Maths_kz0gnaa3h8516"
};
var appStatus = await appConnection.OpenAsync();
(appStatus == AppServiceConnectionStatus.Success)
var request =
ValueSet {{
"Add"
}, {
, Value1}, {
, Value2}};
var connectionResponse = await appConnection.SendMessageAsync(request);
(connectionResponse.Status == AppServiceResponseStatus.Success)
var result = connectionResponse.Message;
(result !=
&& result.ContainsKey(
))
ReturnResult =
"Result is : "
+ result[
<Grid Background=
"{ThemeResource ApplicationPageBackgroundThemeBrush}"
>
<Grid.RowDefinitions>
<RowDefinition Height=
"Auto"
/>
</Grid.RowDefinitions>
<Grid Margin=
"5,30,0,0"
Grid.Row=
"1"
<StackPanel Grid.Row=
"0"
<TextBlock Text=
"Enter the Value1"
<TextBox Text=
"{x:Bind Value1,Mode=TwoWay}"
PlaceholderText=
"First Number"
</StackPanel>
"Enter the Value2"
"{x:Bind Value2,Mode=TwoWay}"
"Second Number"
"2"
<Button x:Name=
"BtnSend"
Content=
"Send"
Margin=
"5,10,2,2"
Click=
"BtnSend_Click"
"{x:Bind ReturnResult,Mode=TwoWay}"
FontSize=
"25"
FontWeight=
"ExtraBold"
"5,15,0,0"
</Grid>
Download sample : AppService
Hope you understand, how to implement the App Service concept in Universal window program. Please share your feedback & Suggestions Thanks & Regards, Vinoth.