in src/Microsoft.Extensions.Configuration.AzureAppConfiguration/ConfigurationClientManager.cs [246:303]
private async Task RefreshFallbackClients(CancellationToken cancellationToken)
{
IEnumerable<SrvRecord> srvTargetHosts = Enumerable.Empty<SrvRecord>();
try
{
srvTargetHosts = await _srvLookupClient.QueryAsync(_endpoint.DnsSafeHost, cancellationToken).ConfigureAwait(false);
}
// Catch and log all exceptions thrown by srv lookup client to avoid new possible exceptions on app startup.
catch (SocketException ex)
{
_logger.LogWarning(LogHelper.BuildFallbackClientLookupFailMessage(ex.Message));
return;
}
catch (DnsResponseException ex)
{
_logger.LogWarning(LogHelper.BuildFallbackClientLookupFailMessage(ex.Message));
return;
}
catch (InvalidOperationException ex)
{
_logger.LogWarning(LogHelper.BuildFallbackClientLookupFailMessage(ex.Message));
return;
}
catch (DnsXidMismatchException ex)
{
_logger.LogWarning(LogHelper.BuildFallbackClientLookupFailMessage(ex.Message));
return;
}
var newDynamicClients = new List<ConfigurationClientWrapper>();
// Honor with the DNS based service discovery protocol, but shuffle the results first to ensure hosts can be picked randomly,
// Srv lookup does retrieve trailing dot in the host name, just trim it.
IEnumerable<string> OrderedHosts = srvTargetHosts.Any()
? srvTargetHosts.ToList().Shuffle().SortSrvRecords().Select(r => $"{r.Target.Value.TrimEnd('.')}")
: Enumerable.Empty<string>();
foreach (string host in OrderedHosts)
{
if (!string.IsNullOrEmpty(host) &&
!_clients.Any(c => c.Endpoint.Host.Equals(host, StringComparison.OrdinalIgnoreCase)) &&
IsValidEndpoint(host))
{
var targetEndpoint = new Uri($"https://{host}");
var configClient = _credential == null
? new ConfigurationClient(ConnectionStringUtils.Build(targetEndpoint, _id, _secret), _clientOptions)
: new ConfigurationClient(targetEndpoint, _credential, _clientOptions);
newDynamicClients.Add(new ConfigurationClientWrapper(targetEndpoint, configClient));
}
}
_dynamicClients = newDynamicClients;
_lastFallbackClientRefresh = DateTime.UtcNow;
}