Example #1
0
        /// <summary>
        /// Get and cache all known and available services using service discovery.
        /// </summary>
        private async Task ResolvePollingServices(List <string> serviceIdentifiers)
        {
            var serviceResultTasks = new List <Task>(serviceIdentifiers.Count);

            Logger.LogInformation($"{nameof(ResolvePollingServices)}: Resolving services for polling...");

            foreach (var identifier in serviceIdentifiers)
            {
                Logger.LogInformation($"{nameof(ResolvePollingServices)}: Testing service '{identifier}'...");

                serviceResultTasks
                .Add(_serviceResolution.ResolveService(serviceName: identifier)
                     .ContinueWith(x =>
                {
                    if (!x.IsCompleted)
                    {
                        Logger.LogError($"{nameof(ResolveServicesState)}: Failure resolving service '{identifier}'; {x.Exception.GetBaseException().Message}");
                        return;
                    }

                    var serviceResult = x.Result;

                    if (string.IsNullOrEmpty(serviceResult.Endpoint) ||
                        serviceResult.Endpoint.Contains("localhost") ||
                        serviceResult.Endpoint.Contains("healthz"))
                    {
                        Logger.LogDebug($"{nameof(ResolvePollingServices)}: Filtering out service '{identifier}' ({serviceResult.Endpoint}) from the polling services list.");

                        return;
                    }

                    Logger.LogInformation($"{nameof(ResolvePollingServices)}: Found service '{identifier}' listening on '{serviceResult.Endpoint}'");

                    _healthCheckState.AddPollingService(new Service(identifier, serviceResult.Endpoint));
                }));
            }

            await Task.WhenAll(serviceResultTasks);
        }