public async Task GetTestElasticSearchConnectionResult() { try { string url = GetQueryStringValueAndAssertIfSingleAndNotEmpty("url"); string authenticationJson = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync(); Authentication authentication = JsonConvert.DeserializeObject <Authentication>(authenticationJson); ElasticClient client = ElasticSearchHelper.CreateClient(new ElasticSearchConnectionString { Nodes = new[] { url }, Authentication = authentication }); PingResponse pingResult = await client.PingAsync(); if (pingResult.IsValid) { DynamicJsonValue result = new() { [nameof(NodeConnectionTestResult.Success)] = true, [nameof(NodeConnectionTestResult.TcpServerUrl)] = url, };
protected virtual string[] VerifiedNodesValueFactory() { var singleLocalNode = new[] { "http://localhost:9200" }; if (TryConnect(singleLocalNode, out var pingResponse)) { return(singleLocalNode); } if (Nodes.Value.Length == 0) { throw new InvalidOperationException($"Environment variable {EnvironmentVariable} is empty"); } if (TryConnect(Nodes.Value, out pingResponse)) { return(Nodes.Value); } throw new InvalidOperationException($"Can't ping Elastic Search instance. Provided urls: {string.Join(',', Nodes.Value)}", pingResponse?.OriginalException); bool TryConnect(string[] nodes, out PingResponse response) { try { var client = ElasticSearchHelper.CreateClient(new ElasticSearchConnectionString { Nodes = nodes }, requestTimeout: TimeSpan.FromSeconds(1), pingTimeout: TimeSpan.FromSeconds(1)); response = client.Ping(); return(response.IsValid); } catch { response = null; return(false); } } }