In Win10, a new and useful feature called App Service,app service allows the app to provide one or more external services for use by other apps without running in the foreground, which looks like a web Api in web development.
By providing services externally, the app can better perform the specialized operations that other apps have, without having to implement the services themselves. Some enterprise users can provide complex services such as cloud recognition and cloud storage for developers to use. This greatly reduces the cost of development and can also bring more users to the service provider. For example, we can call the QR Code recognition service (for example, if the other app provides a QR code service, although the implementation is very simple, here just for example), the image is transferred to the QR Code recognition service, and then the service returns the data we need to show to the user, Then some of the user's cloud data can also use cloud data services to facilitate the operation of cloud data, etc...
Below we are going to create an app with app service, simple implementation to help us calculate two of the sum of the services ( if you want to learn more about app services see: https://msdn.microsoft.com/library/ Windows/apps/xaml/windows.applicationmodel.appservice.aspx) To see how the app service works.
First we create an App Service,app Service is running in the background task, so we need to create in the background task. Create a new Windows Runtime component project that creates a class-integrated background task:
1 Public Sealed classAppservicetask:ibackgroundtask2 {3 Private Staticbackgroundtaskdeferral _servicedeferral;4 Public voidRun (ibackgroundtaskinstance taskinstance)5 {6 //Subscribe to shutdown events7Taskinstance.canceled + =taskinstance_canceled;8 9_servicedeferral =taskinstance.getdeferral ();Ten One varAppservice = Taskinstance.triggerdetails asappservicetriggerdetails; A //Validating callers - if(Appservice.name = ="appserviceuwp-calculate"&& Appservice.name! =NULL) - { the //Subscribe to Caller request -AppService.AppServiceConnection.RequestReceived + =appserviceconnection_requestreceived; - } - } +}
By getting the Triggerdetails property of the background task parameter to convert to the Appservicetriggerdetails object, and then verifying the identity of the downgrade, you can use cryptographic authentication or you can use the secret key verification, which uses the string simple verification, If you subscribe to a request event for a service by validating the caller, the request logic for handling the caller in the event is as follows:
1 Private Async voidappserviceconnection_requestreceived (appserviceconnection sender, Appservicerequestreceivedeventargs args) 2 {3 varMessage =args. Request.message;4 stringCommand = message["Command"] as string;5 Switch(command)6 {7 Case "Calcsum":8 varMessagedeferral =args. Getdeferral ();9 intValue1 = (int) message["Value1"];Ten intvalue2 = (int) message["Value2"]; One varReturnMessage =NewValueSet (); AReturnmessage.add ("Rusult", value1 +value2); - //Responding to callers - varResponseStatus =awaitargs. Request.sendresponseasync (returnmessage); the Messagedeferral.complete (); - Break; - Case "Quit": - _servicedeferral.complete (); + Break; - } +}
Like app to app service, the data passed in app service is also valueset type, we get the value of the command of the Operation command, then choose how to handle the task, and process the result through Sendresponseasync () The Valueset method sends the message containing the data to the caller.
Now that the logic code is complete, we need to declare the app service in the app services provider app, open the Package.appxmanifest file, and declare the app service as follows:
Here we can declare a number of Appservice, formatted like above.
Extension.category set to Appservice type, EntryPoint set to App Service class Namespace.classname,appservice.name set the name of your services, to ensure that the name is unique, the caller needs to know the name, in a formal environment, you can store the name in the UWP's public storage space, so that all apps can access it.
At this point, the server code is complete, and below we create a new project as the caller client. First we place two text boxes and a button on the interface such as:
The background code is as follows:
1 Public Sealed Partial classMainpage:page2 {3 //declaring a Appservice connection object4 Private ReadOnlyAppserviceconnection connection =Newappserviceconnection5 {6Appservicename ="appserviceuwp-calculate",7Packagefamilyname ="B8DF40A7-E5B3-46A2-A65A-28F502255FC5_MD3S7CN435NW2"8 };9 Ten PublicMainPage () One { A This. InitializeComponent (); - This. Loaded + =mainpage_loaded; - } the - Private Async voidMainpage_loaded (Objectsender, RoutedEventArgs e) - { - + varConnectionstatus =awaitconnection. OpenAsync (); - if(Connectionstatus = =appserviceconnectionstatus.success) + { A //subscribe to two-way communication atConnection. Requestreceived + =connection_requestreceived; - } - Else - { - //you can prompt and navigate to the store to let users download the service - await NewMessagedialog ("Service Connection not successful"). Showasync (); in } - } to + Private voidconnection_requestreceived (appserviceconnection sender, Appservicerequestreceivedeventargs args) - { the //To do something * } $ Panax Notoginseng Private Async voidButton_Click (Objectsender, RoutedEventArgs e) - { the varMessage =NewValueSet (); +Message. ADD ("Command","Calcsum"); AMessage. ADD ("Value1",int. Parse (Tbnum1.text)); theMessage. ADD ("Value2",int. Parse (Tbnum2.text)); + -Appserviceresponse response =awaitconnection. Sendmessageasync (message); $ if(Response. Status = =appserviceresponsestatus.success) $ { - stringsum = response. message["Rusult"]. ToString (); - await Newmessagedialog (sum). Showasync (); the } - ElseWuyi { } the } -}
First we declare an app service connection object that assigns the Name of the app service to the Appservice.name,packagefamilyname we set up, we can provide the app service In the app, use the following code to get it.
Debug.WriteLine (Package.Current.Id.FamilyName);
This familyname is for other apps to know in real-world development.
Ok, we connect the app service that specifies name and Familyname, and then turn on the connection pipeline, and if the connection is unsuccessful, you can prompt the user for information based on the connection status return value, and if the app service is not found, you can also direct the user to download the service app.
Then we create a Valueset object in the Click event, add the dictionary data and send it to the server, then wait for the service side to respond.
Let's look at the effect:
Recommend a UWP development group: 53078485 You can come in and study together.
Uwp/win10 new feature series-app Service