UWP AppServiceConnection - SendResponseAsync返回AppServiceResponseStatus.Failure

时间:2018-06-18 11:01:34

标签: c# uwp iot windows-10-iot-core

我试图在Raspberry Pi3上创建一个UWP服务应用程序,该应用程序提供对板载UART的访问。我正面临有关AppConnection请求/响应的问题。

这是处理来自客户端应用程序的传入请求的服务方法

internal class Inbound
{
    public static async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
    {
        var messageDeferral = args.GetDeferral();
        var response = new ValueSet();

        bool success = false;
        var msg = args.Request.Message.Keys;
        if (args.Request.Message.TryGetValue(ServiceApiRequests.Keys.Command, out object command))
        {
            try
            {
                switch (command)
                {

                    case ServiceApiRequests.CommandValues.UartWrite:
                        if (args.Request.Message.TryGetValue(ServiceApiRequests.Keys.UartTxBuffer, out object txBuffer))
                        {
                            string rxBuff = "";
                            success = await Pi3.Peripherals.Uart.GerInstance(57600).Write((string)txBuffer);

                            if (success)
                            {
                                Debug.WriteLine("Tx: " + (string)txBuffer);
                                if (args.Request.Message.TryGetValue(ServiceApiRequests.Keys.ReadUartResponse, out object getResponse))
                                {
                                    if ((string)getResponse == ServiceApiRequests.ReadUartResponse.Yes)
                                    {
                                        rxBuff = await Pi3.Peripherals.Uart.GerInstance(57600).Read();
                                        Debug.WriteLine("Rx: " + rxBuff);
                                    }
                                }
                            }
                            response.Add(ServiceApiRequests.Keys.UartRxBuffer, rxBuff);

                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                success = false;
            }
        }

        response.Add(new KeyValuePair<string, object>(ServiceApiRequests.Keys.Result, success ? ServiceApiRequests.ResultValues.Ok : ServiceApiRequests.ResultValues.Ko));
        var result = await args.Request.SendResponseAsync(response);
        if (result == AppServiceResponseStatus.Failure)
        {
            Debug.WriteLine("Failed to send the response");
        }
        messageDeferral.Complete();
    }
}

正如您所知,Uart类使用方法Pi3.Peripherals.Uart.GerInstance(57600)来使用Singleton模式。

按照我使用的代码发送来自客户端应用程序的请求。

    public static class Uart
{
    public static IAsyncOperation<string> SendCommand(this AppServiceConnection DriverControllerConnection, string txBuffer, string awaitResponse = ServiceApiRequests.ReadUartResponse.Yes)
    {
        return _SendCommand(DriverControllerConnection, txBuffer, awaitResponse).AsAsyncOperation();
    }
    private static async Task<string> _SendCommand(AppServiceConnection DriverControllerConnection, string txBuffer, string awaitResponse)
    {
        AppServiceResponse response = null;
        string response_str = "";
        try
        {
            if (DriverControllerConnection != null)
            {
                response = await DriverControllerConnection.SendMessageAsync(new ServiceApiRequests.UartWrite().GetCommand(txBuffer, awaitResponse));
                if (response.Status == AppServiceResponseStatus.Success)
                {
                    if (response.Message.TryGetValue(ServiceApiRequests.Keys.Result, out object result))
                    {
                        if ((string)result == ServiceApiRequests.ResultValues.Ok && awaitResponse == ServiceApiRequests.ReadUartResponse.Yes)
                        {
                            response_str = response.Message[ServiceApiRequests.Keys.UartRxBuffer] as string;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // TODO: log
        }
        return response_str;
    }
}

系统只运行一段时间,直到我有response.Status == AppServiceResponseStatus.Success,然后请求的结果发生变化,变为AppServiceResponseStatus.Failure。这样,程序计数器永远不会进入条件if (response.Status == AppServiceResponseStatus.Success)

有关原因的任何想法? 非常感谢你的帮助。

修改

按照建议,我为ServiceClosed事件添加了一个处理程序。这是主要的课程。

public sealed class DriverListener : IBackgroundTask
{
    private BackgroundTaskDeferral backgroundTaskDeferral;
    private AppServiceConnection appServiceConnection;
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        backgroundTaskDeferral = taskInstance.GetDeferral();
        // taskInstance.Canceled += OnTaskCanceled;

        var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
        appServiceConnection = triggerDetails.AppServiceConnection;
        appServiceConnection.RequestReceived += Inbound.OnRequestReceived;
        appServiceConnection.ServiceClosed += OnTaskCanceled;
    }

    private void OnTaskCanceled(AppServiceConnection sender, AppServiceClosedEventArgs reason)
    {
        if (this.backgroundTaskDeferral != null)
        {
            Debug.WriteLine("ServiceClosed");
            // Complete the service deferral.
            this.backgroundTaskDeferral.Complete();
        }
    }
}

在此函数中放置一个断点,我发现它从未被触发过。 应用程序连接使用单例模式打开,并放在我在客户端应用程序中使用的dll中

    public static AppServiceConnection GetDriverConnectionInstance()
    {
        if (_DriverConnectionInstance == null)
        {
            try
            {
                _DriverConnectionInstance = OpenDriverConnection().AsTask().GetAwaiter().GetResult();
            }
            catch
            {

            }
        }
        return _DriverConnectionInstance;   
    }

我还向切换led的服务添加了一个请求,我注意到led状态发生了变化,但应用服务的响应仍然是&#34;失败&#34;消息为空。

1 个答案:

答案 0 :(得分:0)

除非前台经验要求,否则AppService的默认生存期为25秒。当服务关闭连接时,您的客户端进程将收到ServiceClosed事件,因此您知道下次您要发送请求时需要重新打开连接。