Overview

edit

DnsClient sends DNS queries to one server, over the transport you choose, and returns the parsed response. It is a client for a resolver or an authoritative server you name: it does not read the DNS configuration of the machine and does not resolve names recursively by itself.

using var client = new DnsClient(serverAddress, DnsClientProtocol.Udp);
var response = await client.QueryAsync("www.example.com", DnsQueryType.A);

Assert.Equal(DnsResponseCode.NoError, response.Header.ResponseCode);
var addresses = response.Answers.OfType<DnsARecord>().Select(record => record.Address).ToList();
Assert.Equal([IPAddress.Parse("192.0.2.10")], addresses);

Pages

  • Protocols: UDP, TCP, DNS over TLS, DNS over HTTPS and DNS over QUIC, the format of the server address, and the HTTP options.
  • Querying: record types, typed records, response codes, reverse lookups, internationalized names, and custom query messages.
  • DNSSEC: request the signatures, read the resolver's AD flag, or validate the chain of trust locally.
  • Errors and timeouts: the exceptions, the timeout, how responses are checked, and tracing.

Options

DnsClientOptions is the third argument of the constructor:

OptionDefaultUse
Timeout5 secondsHow long to wait for a response. Timeout.InfiniteTimeSpan disables it. See Errors and timeouts.
EnableEdnstrueAdds an EDNS(0) OPT record to the queries.
EdnsUdpPayloadSize1232The largest UDP response the client accepts, advertised in the OPT record.
RetryTruncatedOverTcptrueSends the query again over TCP when a UDP response is truncated. See Truncated responses.
DnssecOkfalseAsks for the DNSSEC records. See DNSSEC.
DnssecValidationModeNoneLocal validates the DNSSEC chain of trust. See DNSSEC.
DnssecTrustAnchorsDnssecTrustAnchors.RootThe trust anchors of local validation.
TimeProviderTimeProvider.SystemThe clock used to check the validity period of DNSSEC signatures.
HttpHandler, HttpVersion, HttpVersionPolicya pooled handler, HTTP/2, RequestVersionOrLowerDNS over HTTPS only. See DNS over HTTPS.
ServerAddressResolverthe system resolverResolves the host name of the server. See Server address.