在winform / wpf客户端上调用UWP AppService

时间:2016-07-08 03:09:23

标签: c# wpf winforms windows-services uwp

当我在UWP应用程序上调用AppService时,AppServiceConnectionStatus返回Success。 但是,当我在winform或wpf客户端上调用AppService时,AppServiceConnectionStatus仍然返回AppServiceUnavailable。 UWP,Winform,WPF,客户端代码是相同的:

private AppServiceConnection appService;
private async void ConnectServer()
{
    if (appService == null)
    {
        appService = new AppServiceConnection();
        appService.AppServiceName = "AserSecurityService";
        appService.PackageFamilyName = "AserSecurityService_gfeg8w3smza92";

        var status = await this.appService.OpenAsync();
        if (status != AppServiceConnectionStatus.Success)
        {
            appService = null;
            return;
        }
    }
}

以下服务代码:

public sealed class AserSecurityComponentProviderTask : IBackgroundTask
{
    private BackgroundTaskDeferral backgroundTaskDeferral;
    private AppServiceConnection appServiceconnection;

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        this.backgroundTaskDeferral = taskInstance.GetDeferral();

        taskInstance.Canceled += OnTaskCanceled;

        var details = taskInstance.TriggerDetails as AppServiceTriggerDetails;
        appServiceconnection = details.AppServiceConnection;
        appServiceconnection.RequestReceived += OnRequestReceived;
    }

    private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
    {
        var requestDeferral = args.GetDeferral();

        try
        {
            var message = args.Request.Message;

            var result = new ValueSet();

            if (message.ContainsKey("HardwareID"))
            {
                result.Add("HardwareID", GetHardwareID());
            }

            await args.Request.SendResponseAsync(result);
        }
        catch (Exception ex)
        {
            var result = new ValueSet();

            result.Add("exception", ex);

            await args.Request.SendResponseAsync(result);
        }
        finally
        {
            requestDeferral.Complete();
        }
    }

    private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
    {
        if (this.backgroundTaskDeferral != null)
        {
            this.backgroundTaskDeferral.Complete();
        }
    }

    private string GetHardwareID()
    {
        if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.System.Profile.HardwareIdentification"))
        {
            var token = HardwareIdentification.GetPackageSpecificToken(null);
            var hardwareId = token.Id;
            var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);

            byte[] bytes = new byte[hardwareId.Length];
            dataReader.ReadBytes(bytes);

            return BitConverter.ToString(bytes).Replace("-", "");
        }

        throw new Exception("NO API FOR DEVICE ID PRESENT!");
    }
}

我该如何解决这个问题?

或者您可以从中下载我的示例项目代码zip hereotherLink

0 个答案:

没有答案