using Microsoft.Extensions.Options; using Microsoft.Extensions.ServiceDiscovery.Abstractions; namespace HelloShop.ApiService.Infrastructure; public class ConfiguredServiceEndPointResolver(IConfiguration configuration, IOptions resolverOptions) : IConfiguredServiceEndPointResolver { private readonly Lazy> _serviceEndPoints = new(() => { ArgumentException.ThrowIfNullOrWhiteSpace(resolverOptions.Value.SectionName, nameof(resolverOptions.Value.SectionName)); IConfigurationSection section = configuration.GetRequiredSection(resolverOptions.Value.SectionName); List serviceEndPoints = []; foreach (IConfigurationSection serviceSection in section.GetChildren()) { string serviceName = serviceSection.Key; serviceEndPoints.Add(new ConfiguredServiceEndPoint { ServiceName = serviceName, Endpoints = serviceSection.GetChildren().SelectMany(x => x.Get>() ?? []).Distinct().ToList() }); } return serviceEndPoints; }); public async Task> GetConfiguredServiceEndpointsAsync(CancellationToken cancellationToken = default) { return await Task.FromResult(_serviceEndPoints.Value); } }