UWP使用AppService向另一个UWP客户端应用程序提供服务
在上篇里,我使用的是寄宿在WPF上的WCF進行兩個程序間的通信,在解決問題的同時,我的同事也在思考能否使用UWP來做這件事。于是,我們發現了App Service,兩個UWP應用溝通的橋梁。
App Service以background task 的形式允許一個UWP向其它UWP提供服務。
首先我們新建一個名為"MyCalculatorService"的Windows Runtime Component項目,新建Calculator類,實現 IBackgroundTask.接口,它很類似WCF里的ServiceContract。
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");break;}case "Subtract":{result = firstNumber - secondNumber;returnData.Add("Result", result.ToString());returnData.Add("Status", "Complete");break;}case "Multiply":{result = firstNumber * secondNumber;returnData.Add("Result", result.ToString());returnData.Add("Status", "Complete");break;}case "Divide":{result = firstNumber / secondNumber;returnData.Add("Result", result.ToString());returnData.Add("Status", "Complete");break;}default:{returnData.Add("Status", "Fail: unknown command");break;}}}await args.Request.SendResponseAsync(returnData);messageDeferral.Complete();}private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason){if (this.backgroundTaskDeferral != null){this.backgroundTaskDeferral.Complete();}}}然后再新建一個名為MyCalculatorServiceProvider的UWP程序,充當服務端角色,相當于WCF宿主服務的。引用剛才我們創建的wind,然后在?Package.appxmanifest 中聲明一個 名為CalculatorService1的App Service 實例,添加 入口點"MyCalculatorService.Calculator"。
現在我們來創建名為“CalculatorClient”的客戶端,并調用以上服務。添加以下代碼
public sealed partial class MainPage : Page{private AppServiceConnection calculatorService;public MainPage(){this.InitializeComponent();}private async void button_Click(object sender, RoutedEventArgs e){//Add the connectionif (calculatorService == null){calculatorService = new AppServiceConnection();calculatorService.AppServiceName = "CalculatorService1";calculatorService.PackageFamilyName = "83da5395-2473-49fb-b361-37072e87e9b9_xe3s0d4n4696a";var status = await calculatorService.OpenAsync();if (status != AppServiceConnectionStatus.Success){Result.Content = "Failed to connect";return;}}//Call the serviceint num1 = int.Parse(InputtextBox1.Text);int num2 = int.Parse(InputtextBox2.Text);var message = new ValueSet();message.Add("Command", Operation.SelectionBoxItem);message.Add("Input1", num1);message.Add("Input2", num2);AppServiceResponse response = await calculatorService.SendMessageAsync(message);string result = "";if (response.Status == AppServiceResponseStatus.Success){//Get the data that the service sentif (response.Message["Status"] as string == "Complete"){result = response.Message["Result"] as string;}}message.Clear();ResulttextBlock.Text = result;}}注意其中的AppServiceName是我們在MyCalculatorServiceProvider項目中定義的App Service的Name,PackageFamilyName 是MyCalculatorServiceProvider項目的PackageFamilyName。
完成后,先部署MyCalculatorServiceProvider再部署CalculatorClient,效果是不是跟WCF很類似呢?
好吧,以上都是我從https://social.technet.microsoft.com/wiki/contents/articles/36719.wcf-app-services-in-universal-windows-platform-uwp-using-windows-10.aspx抄的,,,
示例demo可以從這里下http://www.cnblogs.com/luquanmingren/p/7692305.html,沒錯,我就是懶
?
轉載于:https://www.cnblogs.com/StartAllOverAgain/p/7692555.html
總結
以上是生活随笔為你收集整理的UWP使用AppService向另一个UWP客户端应用程序提供服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 博客园的新手一枚
- 下一篇: SSIM(structural simi