.NET 11 Preview - Native DNS Record Resolving
[C#, .NET, .NET 11 Preview]
If you are doing some very low level networking work, perhaps at DNS level, one challenge you will run into is when you need to do your own DNS resolution.
This is not natively available in .NET, and you therefore have to resort to one of the following:
- Shell to a command like nslookup or dig and parse the results
- Use interop (if on Windows) from the
dnsapi.dlllibrary - Write the low level code to send data and parse results over sockets
- Use a third party library, like DNSClient
This has been addressed in .NET 11 using the new ResolveSrvAsync method.
The code is like this:
var result = await Dns.ResolveSrvAsync("_imaps._tcp.gmail.com");
if (result.ResponseCode == DnsResponseCode.NoError)
{
if (result.Records.Count == 0)
{
Console.WriteLine("No SRV records found.");
}
else
{
foreach (var record in result.Records)
{
Console.WriteLine($"{record.Target}:{record.Port} Priority={record.Priority} Weight={record.Weight}");
}
}
}
The return type, result is of type DnsResult<SrvRecord>.
This should return the following:

There are equivalent methods revolving other types of record:
These also have equivalent synchronous versions.
NOTE: This only works in Windows, at present.
TLDR
.NET 11 can now do native DNS resolution and parse the returned records for subsequent processing.
The code is in my GitHub.
Happy hacking!