Original: UWP uses Appservice to provide services to another UWP client application
In the previous article, I was using WCF hosted on WPF to communicate between two programs, and while I was solving the problem, my colleagues were wondering if they could use the UWP to do it. As a result, we found a bridge between app Service and two UWP apps.
APP service allows one UWP to serve other uwp in the form of background task.
First we create a new Windows Runtime component project called "Mycalculatorservice", a new calculator class that implements Ibackgroundtask. Interface, which is similar to the ServiceContract in WCF.
public sealed class Calculator:ibackgroundtask{private backgroundtaskdeferral backgroundtaskdeferral;private Appserviceconnection appserviceconnection;public void Run (Ibackgroundtaskinstance taskinstance) { This.backgroundtaskdeferral = Taskinstance.getdeferral (); var details = Taskinstance.triggerdetails as Appservicetriggerdetails;appserviceconnection = details. Appserviceconnection;appserviceconnection.requestreceived + = onrequestreceived;taskinstance.canceled + = ontaskcanceled;} Private async void Onrequestreceived (appserviceconnection sender, Appservicerequestreceivedeventargs args) {var Messagedeferral = args. Getdeferral (); ValueSet message = args. Request.message; ValueSet returndata = new ValueSet (); string command = message["command"] as String; ADD, Subtract, Multiply, Divideint? Firstnumber = message["INPUT1"] as int?; Int? Secondnumber = message["Input2"] as int?; Int? result = 0;if (Firstnumber.hasvalue && secondnumber.hasvalue) {switch (command) {case "Add": {result = fIrstnumber + secondnumber;returndata.add ("result", result.) ToString ()); Returndata.add ("Status", "complete"); Case "Subtract": {result = Firstnumber-secondnumber;returndata.add ("result", result.) ToString ()); Returndata.add ("Status", "complete"); Case "Multiply": {result = Firstnumber * Secondnumber;returndata.add ("result", result.) ToString ()); Returndata.add ("Status", "complete"); Case "Divide": {result = Firstnumber/secondnumber;returndata.add ("result", result.) ToString ()); Returndata.add ("Status", "complete"); Default:{returndata.add ("Status", "Fail:unknown Command"); await args. Request.sendresponseasync (Returndata); Messagedeferral.complete ();} private void ontaskcanceled (ibackgroundtaskinstance sender, Backgroundtaskcancellationreason reason) {if ( This.backgroundtaskdeferral! = null) {This.backgroundTaskDeferral.Complete ();}}}
Then create a new UWP program named Mycalculatorserviceprovider, which acts as a server-side role, equivalent to the WCF hosting service. Referring to the wind we just created, and then declaring an app Service instance named CalculatorService1 in Package.appxmanifest, add the entry point " Mycalculatorservice.calculator ".
Now let's create a client named "Calculatorclient" and invoke the service above. Add the following code
Public Sealed Partial classMainpage:page {Privateappserviceconnection CalculatorService; PublicMainPage () { This. InitializeComponent (); } Private Async voidButton_Click (Objectsender, RoutedEventArgs e) { //ADD the connection if(CalculatorService = =NULL) {CalculatorService=Newappserviceconnection (); Calculatorservice.appservicename="CalculatorService1"; Calculatorservice.packagefamilyname="83da5395-2473-49fb-b361-37072e87e9b9_xe3s0d4n4696a"; varStatus =awaitCalculatorservice.openasync (); if(Status! =appserviceconnectionstatus.success) {result.content="Failed to connect"; return; } } //Call the service intNUM1 =int. Parse (Inputtextbox1.text); intnum2 =int. Parse (Inputtextbox2.text); varMessage =NewValueSet (); Message. ADD ("Command", Operation.selectionboxitem); Message. ADD ("INPUT1", NUM1); Message. ADD ("Input2", num2); Appserviceresponse Response=awaitcalculatorservice.sendmessageasync (message); stringresult =""; if(Response. Status = =appserviceresponsestatus.success) {//Get The data that the service sent if(Response. message["Status"] as string==" Complete") {result= Response. message["Result"] as string; }} message. Clear (); Resulttextblock.text=result; } }
Note that the Appservicename is the app service that we defined in the Mycalculatorserviceprovider project Name,packagefamilyname Is the packagefamilyname of the Mycalculatorserviceprovider project.
When you're done, deploy Mycalculatorserviceprovider and deploy Calculatorclient, but is the effect similar to WCF?
All right, all of this is from https://social.technet.microsoft.com/wiki/contents/articles/36719. Wcf-app-services-in-universal-windows-platform-uwp-using-windows-10.aspx copy,,,
Example demo can be http://www.cnblogs.com/luquanmingren/p/7692305.html from here, yes, I'm lazy.
UWP uses Appservice to provide services to another UWP client application