Class DnsQueryExtensions
Extension methods for the IDnsQuery contract.
The methods implement common queries which are more complex and have some business logic.
Testing
Inherited Members
Namespace: DnsClient
Assembly: DnsClient.dll
Syntax
public static class DnsQueryExtensions
Methods
| Improve this Doc View SourceGetHostEntry(IDnsQuery, IPAddress)
The GetHostEntry
method does a reverse lookup on the IP address
,
and queries a DNS server for the IP addresses and aliases associated with the resolved hostname.
IP addresses found are returned in AddressList.
CNAME records are used to populate the Aliases.
The HostName property will be set to the resolved hostname of the address
.
Declaration
public static IPHostEntry GetHostEntry(this IDnsQuery query, IPAddress address)
Parameters
Type | Name | Description |
---|---|---|
IDnsQuery | query | The IDnsQuery instance. |
IPAddress | address | The IPAddress to query for. |
Returns
Type | Description |
---|---|
IPHostEntry | An IPHostEntry instance that contains address information about the host specified in |
Remarks
The method has some logic to populate the Aliases list:
- In case of sub-domain queries or similar, there might be multiple CNAME records for one IPAddress,
- If only one IPAddress is in the result set, all the aliases found will be returned.
- If more than one IPAddress is in the result set, aliases are returned only if at least one doesn't match the queried hostname.
Examples
The following code example uses the GetHostEntry(IDnsQuery, IPAddress) method to resolve an IP address to an IPHostEntry instance.
public static void PrintHostEntry(IPAddress address)
{
var lookup = new LookupClient();
IPHostEntry hostEntry = lookup.GetHostEntry(address);
Console.WriteLine(hostEntry.HostName);
foreach (var ip in hostEntry.AddressList)
{
Console.WriteLine(ip);
}
foreach (var alias in hostEntry.Aliases)
{
Console.WriteLine(alias);
}
}
Exceptions
Type | Condition |
---|---|
ArgumentNullException | If |
DnsResponseException | In case ThrowDnsErrors is set to true and a DNS error occurs. |
GetHostEntry(IDnsQuery, String)
The GetHostEntry
method queries a DNS server for the IP addresses and aliases associated with the hostNameOrAddress
.
In case hostNameOrAddress
is an IPAddress, GetHostEntry
does a reverse lookup on that first to determine the hostname.
IP addresses found are returned in AddressList.
CNAME records are used to populate the Aliases.
The HostName property will be set to the resolved hostname or hostNameOrAddress
.
Declaration
public static IPHostEntry GetHostEntry(this IDnsQuery query, string hostNameOrAddress)
Parameters
Type | Name | Description |
---|---|---|
IDnsQuery | query | The IDnsQuery instance. |
String | hostNameOrAddress | The IPAddress or host name to query for. |
Returns
Type | Description |
---|---|
IPHostEntry | An IPHostEntry instance that contains address information about the host specified in |
Remarks
The method has some logic to populate the Aliases list:
- In case of sub-domain queries or similar, there might be multiple CNAME records for one IPAddress,
- If only one IPAddress is in the result set, all the aliases found will be returned.
- If more than one IPAddress is in the result set, aliases are returned only if at least one doesn't match the queried hostname.
Examples
The following code example uses the GetHostEntry(IDnsQuery, String) method to resolve an IP address or hostname to an IPHostEntry instance.
public static void PrintHostEntry(string hostOrIp)
{
var lookup = new LookupClient();
IPHostEntry hostEntry = lookup.GetHostEntry(hostOrIp);
Console.WriteLine(hostEntry.HostName);
foreach (var ip in hostEntry.AddressList)
{
Console.WriteLine(ip);
}
foreach (var alias in hostEntry.Aliases)
{
Console.WriteLine(alias);
}
}
Exceptions
Type | Condition |
---|---|
ArgumentNullException | If |
DnsResponseException | In case ThrowDnsErrors is set to true and a DNS error occurs. |
GetHostEntryAsync(IDnsQuery, IPAddress)
The GetHostEntryAsync
method does a reverse lookup on the IP address
,
and queries a DNS server for the IP addresses and aliases associated with the resolved hostname.
IP addresses found are returned in AddressList.
CNAME records are used to populate the Aliases.
The HostName property will be set to the resolved hostname of the address
.
Declaration
public static Task<IPHostEntry> GetHostEntryAsync(this IDnsQuery query, IPAddress address)
Parameters
Type | Name | Description |
---|---|---|
IDnsQuery | query | The IDnsQuery instance. |
IPAddress | address | The IPAddress to query for. |
Returns
Type | Description |
---|---|
Task<IPHostEntry> | An IPHostEntry instance that contains address information about the host specified in |
Remarks
The method has some logic to populate the Aliases list:
- In case of sub-domain queries or similar, there might be multiple CNAME records for one IPAddress,
- If only one IPAddress is in the result set, all the aliases found will be returned.
- If more than one IPAddress is in the result set, aliases are returned only if at least one doesn't match the queried hostname.
Examples
The following code example uses the GetHostEntryAsync(IDnsQuery, IPAddress) method to resolve an IP address to an IPHostEntry instance.
public static async Task PrintHostEntry(IPAddress address)
{
var lookup = new LookupClient();
IPHostEntry hostEntry = await lookup.GetHostEntryAsync(address);
Console.WriteLine(hostEntry.HostName);
foreach (var ip in hostEntry.AddressList)
{
Console.WriteLine(ip);
}
foreach (var alias in hostEntry.Aliases)
{
Console.WriteLine(alias);
}
}
Exceptions
Type | Condition |
---|---|
ArgumentNullException | If |
DnsResponseException | In case ThrowDnsErrors is set to true and a DNS error occurs. |
GetHostEntryAsync(IDnsQuery, String)
The GetHostEntryAsync
method queries a DNS server for the IP addresses and aliases associated with the hostNameOrAddress
.
In case hostNameOrAddress
is an IPAddress, GetHostEntry
does a reverse lookup on that first to determine the hostname.
IP addresses found are returned in AddressList.
CNAME records are used to populate the Aliases.
The HostName property will be set to the resolved hostname or hostNameOrAddress
.
Declaration
public static Task<IPHostEntry> GetHostEntryAsync(this IDnsQuery query, string hostNameOrAddress)
Parameters
Type | Name | Description |
---|---|---|
IDnsQuery | query | The IDnsQuery instance. |
String | hostNameOrAddress | The IPAddress or host name to query for. |
Returns
Type | Description |
---|---|
Task<IPHostEntry> | An IPHostEntry instance that contains address information about the host specified in |
Remarks
The method has some logic to populate the Aliases list:
- In case of sub-domain queries or similar, there might be multiple CNAME records for one IPAddress,
- If only one IPAddress is in the result set, all the aliases found will be returned.
- If more than one IPAddress is in the result set, aliases are returned only if at least one doesn't match the queried hostname.
Examples
The following code example uses the GetHostEntryAsync(IDnsQuery, String) method to resolve an IP address or hostname to an IPHostEntry instance.
public static async Task PrintHostEntry(string hostOrIp)
{
var lookup = new LookupClient();
IPHostEntry hostEntry = await lookup.GetHostEntryAsync(hostOrIp);
Console.WriteLine(hostEntry.HostName);
foreach (var ip in hostEntry.AddressList)
{
Console.WriteLine(ip);
}
foreach (var alias in hostEntry.Aliases)
{
Console.WriteLine(alias);
}
}
Exceptions
Type | Condition |
---|---|
ArgumentNullException | If |
DnsResponseException | In case ThrowDnsErrors is set to true and a DNS error occurs. |
GetHostName(IDnsQuery, IPAddress)
The GetHostName
method queries a DNS server to resolve the hostname of the address
via reverse lookup.
Declaration
public static string GetHostName(this IDnsQuery query, IPAddress address)
Parameters
Type | Name | Description |
---|---|---|
IDnsQuery | query | The IDnsQuery instance. |
IPAddress | address | The IPAddress to resolve. |
Returns
Type | Description |
---|---|
String | The hostname if the reverse lookup was successful or |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | If |
DnsResponseException | If no host has been found and ThrowDnsErrors is |
GetHostNameAsync(IDnsQuery, IPAddress)
The GetHostNameAsync
method queries a DNS server to resolve the hostname of the address
via reverse lookup.
Declaration
public static Task<string> GetHostNameAsync(this IDnsQuery query, IPAddress address)
Parameters
Type | Name | Description |
---|---|---|
IDnsQuery | query | The IDnsQuery instance. |
IPAddress | address | The IPAddress to resolve. |
Returns
Type | Description |
---|---|
Task<String> | The hostname if the reverse lookup was successful or |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | If |
DnsResponseException | If no host has been found and ThrowDnsErrors is |
ResolveService(IDnsQuery, String, String, ProtocolType)
The ResolveService
method does a SRV lookup for {
and aggregates the result (hostname, port and list of IPAddresss) to a ServiceHostEntry.
serviceName
}[.{protocol
}].{baseDomain
}
This method expects matching A or AAAA records to populate the AddressList, and/or a CNAME record to populate the HostName property of the result.
Declaration
public static ServiceHostEntry[] ResolveService(this IDnsQuery query, string baseDomain, string serviceName, ProtocolType protocol)
Parameters
Type | Name | Description |
---|---|---|
IDnsQuery | query | The IDnsQuery instance. |
String | baseDomain | The base domain, which will be appended to the end of the query string. |
String | serviceName | The name of the service to look for. Must not have any |
ProtocolType | protocol | The protocol of the service to query for. Set it to Unknown or Unspecified to not pass any protocol. |
Returns
Type | Description |
---|---|
ServiceHostEntry[] | A collection of ServiceHostEntrys. |
Remarks
The returned list of IPAddresss and/or the hostname can be empty if no matching additional records are found.
Exceptions
Type | Condition |
---|---|
ArgumentNullException | If |
See Also
| Improve this Doc View SourceResolveService(IDnsQuery, String, String, String)
The ResolveService
method does a SRV lookup for {
and aggregates the result (hostname, port and list of IPAddresss) to a ServiceHostEntry.
serviceName
}[.{tag
}].{baseDomain
}
This method expects matching A or AAAA records to populate the AddressList, and/or a CNAME record to populate the HostName property of the result.
Declaration
public static ServiceHostEntry[] ResolveService(this IDnsQuery query, string baseDomain, string serviceName, string tag = null)
Parameters
Type | Name | Description |
---|---|---|
IDnsQuery | query | The IDnsQuery instance. |
String | baseDomain | The base domain, which will be appended to the end of the query string. |
String | serviceName | The name of the service to look for. Must not have any |
String | tag | An optional tag. Must not have any |
Returns
Type | Description |
---|---|
ServiceHostEntry[] | A collection of ServiceHostEntrys. |
Remarks
The returned list of IPAddresss and/or the hostname can be empty if no matching additional records are found.
Exceptions
Type | Condition |
---|---|
ArgumentNullException | If |
See Also
| Improve this Doc View SourceResolveServiceAsync(IDnsQuery, String, String, ProtocolType)
The ResolveServiceAsync
method does a SRV lookup for {
and aggregates the result (hostname, port and list of IPAddresss) to a ServiceHostEntry.
serviceName
}[.{protocol
}].{baseDomain
}
This method expects matching A or AAAA records to populate the AddressList, and/or a CNAME record to populate the HostName property of the result.
Declaration
public static Task<ServiceHostEntry[]> ResolveServiceAsync(this IDnsQuery query, string baseDomain, string serviceName, ProtocolType protocol)
Parameters
Type | Name | Description |
---|---|---|
IDnsQuery | query | The IDnsQuery instance. |
String | baseDomain | The base domain, which will be appended to the end of the query string. |
String | serviceName | The name of the service to look for. Must not have any |
ProtocolType | protocol | The protocol of the service to query for. Set it to Unknown or Unspecified to not pass any protocol. |
Returns
Type | Description |
---|---|
Task<ServiceHostEntry[]> | A collection of ServiceHostEntrys. |
Remarks
The returned list of IPAddresss and/or the hostname can be empty if no matching additional records are found.
Exceptions
Type | Condition |
---|---|
ArgumentNullException | If |
See Also
| Improve this Doc View SourceResolveServiceAsync(IDnsQuery, String, String, String)
The ResolveServiceAsync
method does a SRV lookup for {
and aggregates the result (hostname, port and list of IPAddresss) to a ServiceHostEntry.
serviceName
}[.{tag
}].{baseDomain
}
This method expects matching A or AAAA records to populate the AddressList, and/or a CNAME record to populate the HostName property of the result.
Declaration
public static Task<ServiceHostEntry[]> ResolveServiceAsync(this IDnsQuery query, string baseDomain, string serviceName, string tag = null)
Parameters
Type | Name | Description |
---|---|---|
IDnsQuery | query | The IDnsQuery instance. |
String | baseDomain | The base domain, which will be appended to the end of the query string. |
String | serviceName | The name of the service to look for. Must not have any |
String | tag | An optional tag. Must not have any |
Returns
Type | Description |
---|---|
Task<ServiceHostEntry[]> | A collection of ServiceHostEntrys. |
Remarks
The returned list of IPAddresss and/or the hostname can be empty if no matching additional records are found.
Exceptions
Type | Condition |
---|---|
ArgumentNullException | If |