IPHostResolverOptions Class

Namespace: System.Net.IP

Provides options for controlling IP host resolution behavior. This class is used in conjunction with the IPHostResolver class to specify how hostnames are resolved to IP addresses.

Syntax

public class IPHostResolverOptions

Remarks

The IPHostResolverOptions class allows developers to customize the network name resolution process. This can be useful for scenarios such as preferring IPv4 or IPv6 addresses, controlling DNS caching behavior, or specifying custom DNS servers.

Fields

The IPHostResolverOptions class does not expose any public fields. Configuration is typically done through its properties.

Properties

Name Description
EnableCacheLookup Gets or sets a value that indicates whether to perform DNS cache lookups.
UseOnlyIPv4 Gets or sets a value that indicates whether to use only IPv4 addresses during resolution.
UseOnlyIPv6 Gets or sets a value that indicates whether to use only IPv6 addresses during resolution.
DnsServers Gets or sets a collection of custom DNS server IP addresses to use for resolution.

Methods

Name Description
IPHostResolverOptions() Initializes a new instance of the IPHostResolverOptions class with default settings.

Example


using System;
using System.Net;
using System.Net.Sockets;

public class Example
{
    public static async Task ResolveHostname(string hostName)
    {
        // Create an instance of IPHostResolver
        var resolver = new IPHostResolver();

        // Configure options to prefer IPv4 and disable cache lookup
        var options = new IPHostResolverOptions
        {
            UseOnlyIPv4 = true,
            EnableCacheLookup = false
        };

        try
        {
            // Resolve the hostname with the specified options
            IPHostEntry hostEntry = await resolver.ResolveAsync(hostName, options);

            Console.WriteLine($"Resolved '{hostName}':");
            foreach (IPAddress ipAddress in hostEntry.AddressList)
            {
                Console.WriteLine($"- {ipAddress}");
            }
        }
        catch (SocketException ex)
        {
            Console.WriteLine($"Error resolving '{hostName}': {ex.Message}");
        }
    }

    public static async Task Main(string[] args)
    {
        await ResolveHostname("www.microsoft.com");
    }
}
            

Requirements

Assembly: System.Net.Primitives.dll (in .NET Core and .NET 5+)

Assembly: System.dll (in .NET Framework)

EnableCacheLookup Property

Gets or sets a value that indicates whether to perform DNS cache lookups.
public bool EnableCacheLookup { get; set; }

When true, the resolver will check the local DNS cache for existing entries. When false, the resolver will always perform a fresh DNS query.

UseOnlyIPv4 Property

Gets or sets a value that indicates whether to use only IPv4 addresses during resolution.
public bool UseOnlyIPv4 { get; set; }

If set to true, the resolver will only return IPv4 addresses. If false, both IPv4 and IPv6 addresses may be returned based on system configuration and DNS records.

UseOnlyIPv6 Property

Gets or sets a value that indicates whether to use only IPv6 addresses during resolution.
public bool UseOnlyIPv6 { get; set; }

If set to true, the resolver will only return IPv6 addresses. If false, both IPv4 and IPv6 addresses may be returned based on system configuration and DNS records.

DnsServers Property

Gets or sets a collection of custom DNS server IP addresses to use for resolution.
public IList<IPAddress> DnsServers { get; set; }

By default, this property is null, and the system's default DNS servers are used. When populated, the resolver will query these specified DNS servers.

IPHostResolverOptions Constructor

Initializes a new instance of the IPHostResolverOptions class with default settings.
public IPHostResolverOptions()

By default, cache lookup is enabled, and both IPv4 and IPv6 addresses are considered.