DNS APIs in .NET
This section provides documentation on the classes and methods available in the .NET Framework for interacting with the Domain Name System (DNS).
Core DNS Classes
The primary classes for DNS operations reside in the System.Net.Dns
namespace.
System.Net.Dns
Class
Provides methods for resolving domain names and IP addresses. It's the central point for most DNS lookups.
Key Methods:
GetHostName()
Retrieves the friendly name of the local computer.
GetHostEntry(string hostNameOrAddress)
Resolves a host name or IP address to an IPHostEntry
object.
GetHostEntryAsync(string hostNameOrAddress)
Asynchronously resolves a host name or IP address to an IPHostEntry
object.
GetHostAddresses(string hostNameOrAddress)
Retrieves the IP addresses for a host name or IP address.
GetHostAddressesAsync(string hostNameOrAddress)
Asynchronously retrieves the IP addresses for a host name or IP address.
System.Net.IPHostEntry
Class
Represents a DNS host, including its name, aliases, and IP addresses.
Properties:
HostName
: The primary DNS host name.Aliases
: An array of aliases for the host.AddressList
: An array ofIPAddress
objects associated with the host.
System.Net.IPAddress
Class
Represents an Internet Protocol (IP) address.
While not exclusively a DNS class, it's crucial for interpreting DNS query results.
Key Members:
- Static properties like
IPAddress.Any
,IPAddress.Broadcast
,IPAddress.Loopback
. - Methods like
Parse(string s)
andToString()
.
Common DNS Operations and Examples
Resolving a Host Name to an IP Address
This is a fundamental DNS operation, converting a domain name like "www.example.com" into its corresponding IP addresses.
using System;
using System.Net;
public class DnsLookup
{
public static void Main(string[] args)
{
try
{
string hostName = "www.microsoft.com";
IPHostEntry ipHostInfo = Dns.GetHostEntry(hostName);
Console.WriteLine($"Host Name: {ipHostInfo.HostName}");
Console.WriteLine("IP Addresses:");
foreach (IPAddress ipAddress in ipHostInfo.AddressList)
{
Console.WriteLine($"- {ipAddress}");
}
}
catch (SocketException e)
{
Console.WriteLine($"SocketException caught: {e}");
}
catch (Exception e)
{
Console.WriteLine($"General Exception caught: {e}");
}
}
}
Getting the Local Host Name
Retrieve the name of the machine the application is running on.
string localHostName = Dns.GetHostName();
Console.WriteLine($"Local Host Name: {localHostName}");
Asynchronous DNS Operations
For better responsiveness in UI applications, asynchronous methods are recommended.
async Task ResolveHostAsync()
{
try
{
string hostName = "www.google.com";
IPHostEntry ipHostInfo = await Dns.GetHostEntryAsync(hostName);
Console.WriteLine($"Async Host Name: {ipHostInfo.HostName}");
foreach (IPAddress ipAddress in ipHostInfo.AddressList)
{
Console.WriteLine($"- {ipAddress}");
}
}
catch (SocketException e)
{
Console.WriteLine($"SocketException caught: {e}");
}
}
Advanced Topics
Reverse DNS Lookups
While Dns.GetHostEntry(string ipAddress)
performs a reverse lookup if an IP address is provided, explicit reverse lookup mechanisms might be needed for specific scenarios (e.g., using DnsPermission
).
DNS Permissions
Network operations, including DNS lookups, can be controlled using code access security (CAS) permissions, particularly System.Net.DnsPermission
.
Custom DNS Resolution
For advanced scenarios, you might need to bypass the system's default DNS resolution and implement custom logic, possibly by implementing custom DNS client behavior or using lower-level socket operations.