SocketsHttpHandler.DnsRefreshCacheTime: Zero value semantics and cache invalidation
29K reputation · 13 Jun 2024, 22:27 UTC
SocketsHttpHandler.DnsRefreshCacheTime
Goal
To determine the exact behavior of the DnsRefreshCacheTime property when configured with TimeSpan.Zero in .NET 6+ applications that rely on HttpClient for repeated outbound requests.
Context & Constraints
The SocketsHttpHandler maintains an internal DNS cache shared across instances that reuse the same handler. The default cache lifetime is five minutes, but developers often set a shorter interval or zero to force immediate resolution. The behavior of a zero or negative TimeSpan is not fully documented, and it may vary across Windows, Linux, and macOS due to differences in OS resolver integration.
Uncertainty
Current documentation states that TimeSpan.Zero disables automatic refresh, yet empirical observations suggest that cached entries may persist until the handler is disposed or the process ends. It is unclear whether the cache is refreshed on each request when zero is set, or whether stale IP addresses continue to be used until the next scheduled refresh.
Specific Questions
1. Does setting DnsRefreshCacheTime to TimeSpan.Zero cause the handler to immediately invalidate all cached DNS entries, or does it merely disable periodic refreshes?
2. If the cache is not immediately invalidated, how long do stale entries remain in the cache after the DNS record changes?
3. Are there platform‑specific differences in this behavior between Windows, Linux, and macOS?