迁移桌面程序到MS Store(14)——APPX嵌入WCF Service以Admin权限运行

Windows10 1809版本开始,微软又对UWP开放了新的Capability:AllowElevation。 经过这个新的Capability,UWP APP可以在运行时向用户请求Admin权限,配合Windows 1607版本就引入的RunFullTrust Capability(参考《迁移桌面程序到MS Store(9)——APPX With Desktop Extension 》),咱们可让MS Store中的APP拥有无限接近传统桌面程序的能力。
本篇提到的Sample工程位于全球最大的同性交友平台GitHub:
https://github.com/manupstairs/UWPAllowElevationhtml

上图解释了整个程序运行的流程,UWP APP经过RunFullTrust Capability来运行一个exe程序,在这个exe程序中,又由于新的AllowElevation而可以请求Admin权限来启动WCF Servcie。用户在UAC弹窗中确认授予Admin权限后,咱们就在UWP APP的身后拥有了一个具备Admin权限的WCF Service。
首先让咱们建立WCFBackgroundProcess工程,包含一个Hello World级别的WCF Service。在该服务的接口中,咱们定义了三个方法,用来启动,中止和查询Windows Service状态。这么设计是由于这些操做须要Admin权限。git

    [ServiceContract]
    public interface ILocalService
    {

        [OperationContract]
        ServiceControllerStatus StartService(string name);

        [OperationContract]
        ServiceControllerStatus StopService(string name);

        [OperationContract]
        ServiceControllerStatus GetServiceStatus(string name);
    }

WCFBackgroundProcess工程自己是一个Console类型的程序,做为exe启动后,咱们就能够在UWP Client工程中,添加对WCFBackgroundProcess中服务的引用。具体能够参考《迁移桌面程序到MS Store(7)——APPX + Service》。github

    class Program
    {
        static void Main(string[] args)
        {
            var selfHost = new ServiceHost(typeof(LocalServiceWrapper));

            selfHost.Open();

            Console.WriteLine("The service is ready.");

            // Close the ServiceHost to stop the service.
            Console.WriteLine("Press <Enter> to terminate the service.");
            Console.WriteLine();
            Console.ReadLine();
            selfHost.Close();
        }
    }

Launcher工程是一个标准的Console程序,内容很是简单,经过ProcessStartInfo类来启动WCFBackgroundProcess.exe。该工程在UWP环境下所需的AllowElevation咱们在建立Packaging工程时会添加。windows

    class Program
    {
        static void Main(string[] args)
        {
            string result = Assembly.GetExecutingAssembly().Location;
            int index = result.LastIndexOf("\\");
            string rootPath = $"{result.Substring(0, index)}\\..\\";

            rootPath += @"WCFBackgroundProcess\WCFBackgroundProcess.exe";

            ProcessStartInfo info = new ProcessStartInfo
            {
                Verb = "runas",
                UseShellExecute = true,
                FileName = rootPath
            };
            Process.Start(info);
        }
    }

接着建立UWPClient工程,这是一个标准的UWP项目。除了WCFBackgroundService的引用外,咱们还须要经过NuGet添加Windows Desktop Extension for the UWP来实现对Launcher.exe的调用(参考《迁移桌面程序到MS Store(9)——APPX With Desktop Extension 》)。app

 

UWPClient仅包含惟一的MainPage,四个按钮事件分别负责启动Launcher.exe,启动、中止、查询BluetoothService的状态,代码以下:async

    public sealed partial class MainPage : Page
    {
        private string serviceName = "bthserv";
        private LocalServiceClient client = new LocalServiceClient();

        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void StopButton_Click(object sender, RoutedEventArgs e)
        {
            var status = await client.StopServiceAsync(serviceName);
            textBlockStatus.Text = status.ToString();
        }

        private async void StartButton_Click(object sender, RoutedEventArgs e)
        {
            var status = await client.StartServiceAsync(serviceName);
            textBlockStatus.Text = status.ToString();
        }

        private async void QueryButton_Click(object sender, RoutedEventArgs e)
        {
            var status = await client.GetServiceStatusAsync(serviceName);
            textBlockStatus.Text = status.ToString();
        }

        private async void RunWCFService_Click(object sender, RoutedEventArgs e)
        {
            if (ApiInformation.IsApiContractPresent("Windows.ApplicationModel.FullTrustAppContract", 1, 0))
            {
                await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
            }
        }
    }

以上就是全部的准备工做。而后咱们建立做为启动项目的AllowElevationPackaging工程(参考《迁移桌面程序到MS Store(1)——经过Visual Studio建立Packaging工程》)。在AllowElevationPackaging的Applications中包含以前建立的全部三个工程,同时将UWPClient设为Entry Point。学习

右键选中Package.aapxmanifest后进行编辑,在<Application/>节点中添加:this

      <Extensions>
        <desktop:Extension xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10" Category="windows.fullTrustProcess" Executable="Launcher\Launcher.exe" />
      </Extensions>

同时修改Capabilities节点:spa

  <Capabilities>
    <Capability Name="internetClient" />
    <rescap:Capability Name="runFullTrust" />
    <rescap:Capability Name="allowElevation" />
  </Capabilities>

完成后保存关闭文件,大功告成!编译整个解决方案确保没有任何错误,而后让咱们点击F5开始运行(记得将AllowElevationPackaging设为启动项)。
首先咱们先点击Apply for Elevation capability and Run WCF Service按钮。在UAC窗口中咱们赞成给予Admin权限,WCF Service顺利启动。感兴趣的同窗能够经过窗口的标题栏查看WCFBackgroundProcess.exe的物理路径。设计

而后咱们便可以开始查询蓝牙服务的运行状态了。若是用的是蓝牙鼠标,在点击Stop Blue Support Service的同时不要惶恐不安哦。本篇做为《迁移桌面程序到MS Store》系列的最终篇,没人看的系列就准备太监了。犹豫就会败北,果断就会白给。后续博客将专一于.NET Core的学习,与诸君共勉!

GitHub:
https://github.com/manupstairs/UWPAllowElevation

相关文章
相关标签/搜索