DnsClient Class

A DNS client supporting UDP, TCP, DNS over TLS, DNS over HTTPS, DNS over QUIC, DNSSEC, EDNS, IDN, and reverse lookups.

public sealed class DnsClient : IDisposable
Implements

Constructors

NameDescription
DnsClient(string, DnsClientProtocol)Initializes a new instance of the DnsClient class.
DnsClient(string, DnsClientProtocol, DnsClientOptions?)Initializes a new instance of the DnsClient class.

DnsClient overloads

DnsClient(string, DnsClientProtocol)

DnsClient.cs

Initializes a new instance of the DnsClient class.

public DnsClient(string server, DnsClientProtocol protocol)
Parameters
server string
The DNS server address (IP address, hostname, or URL for DNS over HTTPS).
protocol DnsClientProtocol
The DNS transport protocol to use.

DnsClient(string, DnsClientProtocol, DnsClientOptions?)

DnsClient.cs

Initializes a new instance of the DnsClient class.

public DnsClient(string server, DnsClientProtocol protocol, DnsClientOptions? options)
Parameters
server string
The DNS server address (IP address, hostname, or URL for DNS over HTTPS).
protocol DnsClientProtocol
The DNS transport protocol to use.
options DnsClientOptions?
Optional configuration options.

Methods

NameDescription
Dispose()Releases the resources used by this instance.
QueryAsync(string, DnsQueryType, DnsQueryClass, CancellationToken)Sends a DNS query for the specified domain name, record type, and class.
QueryAsync(string, DnsQueryType, CancellationToken)Sends a DNS query for the specified domain name and record type.
ReverseLookupAsync(IPAddress, CancellationToken)Performs a reverse DNS lookup for the specified IP address.
SendAsync(DnsQueryMessage, CancellationToken)Sends a DNS query message and returns the response.

Dispose()

DnsClient.cs

Releases the resources used by this instance.

public void Dispose()

QueryAsync overloads

QueryAsync(string, DnsQueryType, DnsQueryClass, CancellationToken)

DnsClient.cs

Sends a DNS query for the specified domain name, record type, and class.

public Task<DnsResponseMessage> QueryAsync(string name, DnsQueryType type, DnsQueryClass queryClass, CancellationToken cancellationToken = default)
Parameters
name string
The domain name to query. Unicode names are automatically converted to punycode.
type DnsQueryType
The DNS record type to query.
queryClass DnsQueryClass
The DNS query class.
cancellationToken CancellationToken
A cancellation token.
Returns

Task<DnsResponseMessage> The DNS response message.

Exceptions
DnsProtocolException
The response is malformed, or does not answer the query that was sent.
TimeoutException
DnsClientOptions.Timeout elapsed before a response arrived.
OperationCanceledException
cancellationToken was cancelled.
ObjectDisposedException
The client has been disposed.

QueryAsync(string, DnsQueryType, CancellationToken)

DnsClient.cs

Sends a DNS query for the specified domain name and record type.

public Task<DnsResponseMessage> QueryAsync(string name, DnsQueryType type, CancellationToken cancellationToken = default)
Parameters
name string
The domain name to query. Unicode names are automatically converted to punycode.
type DnsQueryType
The DNS record type to query.
cancellationToken CancellationToken
A cancellation token.
Returns

Task<DnsResponseMessage> The DNS response message.

ReverseLookupAsync(IPAddress, CancellationToken)

DnsClient.cs

Performs a reverse DNS lookup for the specified IP address.

public Task<DnsResponseMessage> ReverseLookupAsync(IPAddress address, CancellationToken cancellationToken = default)

Parameters

address IPAddress
The IP address to look up (IPv4 or IPv6).
cancellationToken CancellationToken
A cancellation token.

Returns

Task<DnsResponseMessage> The DNS response message containing PTR records.

SendAsync(DnsQueryMessage, CancellationToken)

DnsClient.cs

Sends a DNS query message and returns the response.

net10.0

public Task<DnsResponseMessage> SendAsync(DnsQueryMessage message, CancellationToken cancellationToken = default)

net11.0

public Task<DnsResponseMessage> SendAsync(DnsQueryMessage message, CancellationToken cancellationToken = default)

Parameters

message DnsQueryMessage
The DNS query message to send.
cancellationToken CancellationToken
A cancellation token.

Returns

Task<DnsResponseMessage> The DNS response message.

Exceptions

DnsProtocolException
The response is malformed, or does not answer the query that was sent.
TimeoutException
DnsClientOptions.Timeout elapsed before a response arrived.
OperationCanceledException
cancellationToken was cancelled.
ObjectDisposedException
The client has been disposed.

Remarks

The message is not modified; the client's options are applied to an internal copy.