/// <summary> /// Receive messages from the other app /// </summary> /// <param name="sender">The connection the message is from</param> /// <param name="args">The arguments for the message</param> private async void RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) { if (!IsValid()) { _log.Information("AppConnection.RequestReceived: called before the connection is valid"); throw new InvalidOperationException("Message received before connection is opened"); } var requestDefferal = args.GetDeferral(); try { _log.Information($"AppConnection.RequestReceived: received the following message: {ValueSetOut.ToString(args.Request.Message)}"); AppServiceResponseStatus status = AppServiceResponseStatus.Unknown; if (MessageReceived != null) { AppMessage lc = AppMessage.FromValueSet(args.Request.Message); AppMessage result = MessageReceived(lc); _log.Information($"AppConnection.RequestRecieved: response: {result.ToString()}"); status = await args.Request.SendResponseAsync(result.ToValueSet()); } _log.Information($"AppConnection.RequestReceived: Response to Request returned: {status.ToString()}"); } finally { requestDefferal.Complete(); } }
/// <summary> /// Send a Command message to the head application /// </summary> /// <param name="command">The command to send</param> /// <returns>The AppMessage containing the response</returns> public IAsyncOperation <AppMessage> SendCommandAsync(AppMessage command) { return(Task <AppMessage> .Run(async() => { AppServiceResponse response = null; AppMessage retCommand; if (IsValid()) { _log.Information($"SendCommandAsync: Sending {command.ToString()}"); response = await Connection.SendMessageAsync(command.ToValueSet()); if (response.Status != AppServiceResponseStatus.Success) { retCommand = new AppMessage( AppMessage.CommandType.Error, $"Command response status: {response.Status} with message: {ValueSetOut.ToString(response.Message)}" ); _log.Error(retCommand.Param.ToString()); } else { retCommand = AppMessage.FromValueSet(response.Message); } } else { _log.Error("SendCommandAsync: called before the connection is valid"); throw new InvalidOperationException("Cannot send a command until connection is opened"); } return retCommand; } ).AsAsyncOperation <AppMessage>()); }