UWP使用AppService向另一个UWP客户端应用程序提供服务

原文: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, Divide
			int? 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 connection
        if (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 service
        int 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 sent
            if (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,没错,我就是懒

UWP使用AppService向另一个UWP客户端应用程序提供服务

上一篇:python-排列组合函数itertools.permutations


下一篇:刷课(刷剧)神器!给网页视频加个速(最快可达16倍!)