UWP AppService发送已取消的SystemPolicy

时间:2017-12-25 21:16:13

标签: uwp

过去3天,我一直试图远程创建和使用Windows 10 UWP AppService。当AppService在一台笔记本电脑上运行而客户端在另一台笔记本电脑(桌面到桌面)上运行时,我就完成了所有工作。但是,当我在手机上安装客户端时,IBackgroundTaskInstance取消原因:SystemPolicy。 我不知道为什么在手机到桌面的情况下它不起作用。 任何人都知道从哪里开始寻找?

以下是我的服务类

using Newtonsoft.Json;
using SharedObjects;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.AppService;
using Windows.ApplicationModel.Background;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Management.Deployment;
using Windows.Storage.Streams;

namespace AppStarterService
{
    public sealed class AppStarterTask : IBackgroundTask
    {
        BackgroundTaskDeferral serviceDeferral;
        AppServiceConnection connection;

        public void Run(IBackgroundTaskInstance taskInstance)
        {
            try
            {
                serviceDeferral = taskInstance.GetDeferral();
                taskInstance.Canceled += OnTaskCanceled;
                var details = taskInstance.TriggerDetails as AppServiceTriggerDetails;
                if (details.Name == "com.poctools.appstarter")
                {
                    connection = details.AppServiceConnection;
                    connection.RequestReceived += OnRequestReceived;
                    connection.ServiceClosed += Connection_ServiceClosed;
                }
                else
                {
                    serviceDeferral.Complete();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }

        async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
        {
            var messageDeferral = args.GetDeferral();
            try
            {
                var input = args.Request.Message;
                var packageList = await GetPackageListInfo();
                var json = JsonConvert.SerializeObject(packageList, Formatting.None);
                var result = new ValueSet();
                result.Add("result", json);
                Debug.WriteLine(json);
                var response = await args.Request.SendResponseAsync(result); //returns Success, even on the phone-to-desktop scenario
                Debug.WriteLine($"Send result: {response}");
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            finally
            {
                messageDeferral.Complete();
            }
        }

        private void Connection_ServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)
        {
            Debug.WriteLine($"Service closed: {args.Status}");
        }

        private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
        {
            Debug.WriteLine($"Task cancelled: {reason}");
            if (serviceDeferral != null)
            {
                serviceDeferral.Complete();
                serviceDeferral = null;
            }

            if (connection != null)
            {
                connection.Dispose();
                connection = null;
            }
        }



        private async Task<List<PackageInfo>> GetPackageListInfo()
        {
            try
            {
                List<PackageInfo> PackageInfoList = new List<PackageInfo>();

                PackageManager packageManager = new PackageManager();
                var Packages = packageManager.FindPackagesForUser("");

                foreach (var package in Packages.Where(p => p.SignatureKind == PackageSignatureKind.Store && !p.IsFramework))
                {
                    IReadOnlyList<AppListEntry> entryList = await package.GetAppListEntriesAsync();
                    if (entryList != null)
                    {
                        foreach (var entry in entryList)
                        {
                            if (entry != null)
                            {
                                Debug.WriteLine(entry.DisplayInfo.DisplayName);
                                var name = entry.DisplayInfo.DisplayName;
                                RandomAccessStreamReference stream = entry.DisplayInfo.GetLogo(new Size(150, 150));
                                if (stream != null)
                                {
                                    var streamContent = await stream.OpenReadAsync();
                                    if (streamContent != null)
                                    {
                                        byte[] buffer = new byte[streamContent.Size];
                                        await streamContent.ReadAsync(buffer.AsBuffer(), (uint)streamContent.Size, InputStreamOptions.None);
                                        string logo = Convert.ToBase64String(buffer);
                                        PackageInfoList.Add(new PackageInfo(name, package.Id.FamilyName, logo));
                                    }
                                }
                            }
                        }
                    }
                    Debug.WriteLine($"{package.Id.Name} - {package.SignatureKind}");
                }
                return PackageInfoList;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

可以在主机和客户端之间发送数据包大小的限制,显然我的手机只有几KB。 GetPackageListInfo()方法获取主机上所有已安装应用程序的列表,其中包含名称和徽标。有些应用程序有很大的标识(Minecraft有一个9 KB的标识,它很大,Paint 3D&#39; s logo是27 KB)。 最后我让服务一次返回1个PackageInfo,让客户端调用服务直到服务返回最后一个PackageInfo,并减少任何大于8KB的徽标。

https://docs.microsoft.com/en-us/windows/uwp/launch-resume/how-to-create-and-consume-an-app-service

的另一个兴趣点

应用服务的生命周期取决于来电者:

  1. 如果呼叫者在前台,则应用服务生命周期是相同的 作为来电者。
  2. 如果来电者在后台,应用服务会获得 运行30秒。取消延期提供额外的延期 时间5秒。
  3. 代码:

        async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
        {
            var messageDeferral = args.GetDeferral();
            try
            {
                var inputs = args.Request.Message;
                if (Singleton.Instance.PackageList == null)
                    Singleton.Instance.PackageList = await GetPackageListInfo();
    
                var itemsSend = inputs["items"] as string[]; //keep track of items already send
                inputs["items"] = null; //clear to keep response data small (it will be filled again when a new request comes in)
                var package = Singleton.Instance.PackageList.FirstOrDefault(p => !itemsSend.Contains(p.Id));
                if (package == null)
                {
                    inputs["status"] = "stop"; //let the client know it's time to stop requesting
                    inputs["package"] = null;
                }
                else
                {
                    var json = JsonConvert.SerializeObject(package);
                    inputs["package"] = json;
                }
                var response = await args.Request.SendResponseAsync(inputs);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            finally
            {
                messageDeferral?.Complete();
            }
        }
    

    ResizeImage

        async Task<byte[]> ResizeImage(byte[] imageData, int reqWidth, int reqHeight)
        {
            var memStream = new MemoryStream(imageData);
    
            using (IRandomAccessStream imageStream = memStream.AsRandomAccessStream())
            {
                //Get BitmapDecoder based on the original image
                var decoder = await BitmapDecoder.CreateAsync(imageStream);
                if (decoder.PixelHeight > reqHeight || decoder.PixelWidth > reqWidth)
                {
                    //BitmapTransform defines a set of transformation to be applied to the original image (we're just interessted in scaling down)
                    BitmapTransform transform = new BitmapTransform()
                    {
                        ScaledHeight = (uint)reqHeight,
                        ScaledWidth = (uint)reqWidth,
                        InterpolationMode = BitmapInterpolationMode.Linear
                    };
    
                    PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
                        BitmapPixelFormat.Rgba8,
                        BitmapAlphaMode.Straight,
                        transform,
                        ExifOrientationMode.IgnoreExifOrientation,
                        ColorManagementMode.ColorManageToSRgb);
    
                    var resizedStream = new InMemoryRandomAccessStream();
                    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, resizedStream);
                    var pixels = pixelData.DetachPixelData();
                    encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, (uint)reqWidth, (uint)reqHeight, 96, 96, pixels);
                    await encoder.FlushAsync();
                    var outBuffer = new byte[resizedStream.Size];
                    await resizedStream.ReadAsync(outBuffer.AsBuffer(), (uint)resizedStream.Size, InputStreamOptions.None);
    
                    return outBuffer;
    
                }
            }
            return imageData;
        }
    

    GetPackageListInfo

        private async Task<List<PackageInfo>> GetPackageListInfo()
        {
            try
            {
                List<PackageInfo> PackageInfoList = new List<PackageInfo>();
    
                PackageManager packageManager = new PackageManager();
                IEnumerable<Package> Packages = packageManager.FindPackagesForUser("");
    
                foreach (var package in Packages.Where(p => p.SignatureKind == PackageSignatureKind.Store && !p.IsFramework))
                {
                    IReadOnlyList<AppListEntry> entryList = await package.GetAppListEntriesAsync();
                    if (entryList != null)
                    {
                        foreach (var entry in entryList)
                        {
                            if (entry != null)
                            {
                                var name = entry.DisplayInfo.DisplayName;
                                RandomAccessStreamReference stream = entry.DisplayInfo.GetLogo(new Size(50, 50));
                                if (stream != null)
                                {
                                    var streamContent = await stream.OpenReadAsync();
                                    if (streamContent != null)
                                    {
                                        byte[] buffer = new byte[streamContent.Size];
                                        await streamContent.ReadAsync(buffer.AsBuffer(), (uint)streamContent.Size, InputStreamOptions.None);
                                        if (streamContent.Size < 9000)
                                        {
                                            PackageInfoList.Add(new PackageInfo(package.Id.FullName, name, package.Id.FamilyName, buffer));
                                        }
                                        else
                                        {
                                            byte[] resizedBuffer = await ResizeImage(buffer, 50, 50);
                                            if (resizedBuffer.Length < 8192)//8kb
                                                PackageInfoList.Add(new PackageInfo(package.Id.FullName, name, package.Id.FamilyName, resizedBuffer));
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //Debug.WriteLine($"{package.Id.Name} - {package.SignatureKind}");
                }
                return PackageInfoList;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }