public async Task HandleError(IConnection Connection, object command, Exception commandErrorException) { Connection.IsNotNull($"Invalid parameter received in the {nameof(Handle)} method. {nameof(Connection)}"); command.IsNotNull($"Invalid parameter received in the {nameof(Handle)} method. {nameof(command)}"); commandErrorException.IsNotNull($"Invalid parameter received in the {nameof(Handle)} method. {nameof(commandErrorException)}"); GetServiceCommand getServiceCommand = command as GetServiceCommand; getServiceCommand.IsNotNull($"Unexpected command received in the {nameof(Handle)} method. {nameof(command)}"); GetServiceCompletion.PayloadData.CompletionCodeEnum errorCode = GetServiceCompletion.PayloadData.CompletionCodeEnum.InternalError; if (commandErrorException.GetType() == typeof(InvalidDataException)) { errorCode = GetServiceCompletion.PayloadData.CompletionCodeEnum.InvalidData; } GetServiceCompletion.PayloadData payLoad = new GetServiceCompletion.PayloadData(errorCode, commandErrorException.Message); await Connection.SendMessageAsync(new GetServiceCompletion(getServiceCommand.Headers.RequestId, payLoad)); }
private async void ServiceDiscovery_Click(object sender, EventArgs e) { int[] PortRanges = new int[] { 80, // Only for HTTP 443, // Only for HTTPS 5846, 5847, 5848, 5849, 5850, 5851, 5852, 5853, 5854, 5855, 5856 }; string commandString = string.Empty; string responseString = string.Empty; string cardServiceURI = string.Empty; textBoxCommand.Text = commandString; textBoxResponse.Text = responseString; textBoxCardReader.Text = cardServiceURI; textBoxEvent.Text = string.Empty; ServicePort = null; foreach (int port in PortRanges) { try { WebSocketState state; using (var socket = new ClientWebSocket()) { var cancels = new CancellationTokenSource(); cancels.CancelAfter(400); await socket.ConnectAsync(new Uri($"{textBoxServiceURI.Text}:{port}/xfs4iot/v1.0"), cancels.Token); state = socket.State; } if (state == WebSocketState.Open) { ServicePort = port; var Discovery = new XFS4IoTClient.ClientConnection(new Uri($"{textBoxServiceURI.Text}:{ServicePort}/xfs4iot/v1.0")); try { await Discovery.ConnectAsync(); } catch (Exception) { continue; } var getServiceCommand = new GetServiceCommand(Guid.NewGuid().ToString(), new GetServiceCommand.PayloadData(CommandTimeout)); commandString = getServiceCommand.Serialise(); await Discovery.SendCommandAsync(getServiceCommand); object cmdResponse = await Discovery.ReceiveMessageAsync(); if (cmdResponse is GetServiceCompletion response) { responseString = response.Serialise(); var service = (from ep in response.Payload.Services where ep.ServiceUri.Contains("CardReader") select ep ).FirstOrDefault() ?.ServiceUri; if (!string.IsNullOrEmpty(service)) { cardServiceURI = service; } } break; } } catch (WebSocketException) { } catch (System.Net.HttpListenerException) { } catch (TaskCanceledException) { } } if (ServicePort is null) { textBoxPort.Text = ""; MessageBox.Show("Failed on finding services."); } else { textBoxPort.Text = ServicePort.ToString(); } textBoxCommand.Text = commandString; textBoxResponse.Text = responseString; textBoxCardReader.Text = cardServiceURI; }