DnsNameResolver 1.0.1

dotnet add package DnsNameResolver --version 1.0.1
                    
NuGet\Install-Package DnsNameResolver -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DnsNameResolver" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DnsNameResolver" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="DnsNameResolver" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DnsNameResolver --version 1.0.1
                    
#r "nuget: DnsNameResolver, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DnsNameResolver@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DnsNameResolver&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=DnsNameResolver&version=1.0.1
                    
Install as a Cake Tool

DnsResolver

C# async wrapper for DnsQueryEx using C++ to call native method.

Example

/// <summary>
/// Callback function for <see cref="ResolveDnsName(string, DnsRecordTypes, CallbackDelegate)"/>.
/// </summary>
/// <param name="status">DNS query result code.</param>
/// <param name="ipAddress">Retrieved IP Address.</param>
/// <param name="ttl">Time to live of retrieved IP Address.</param>
/// <remarks>
/// LONG is the same size as int in Windows.
/// </remarks>
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void CallbackDelegate(int status, string ipAddress, uint ttl);

public enum DnsRecordTypes: ushort
{
    DNS_TYPE_A = 0x0001, // IPv4 record
    DNS_TYPE_AAAA = 0x001c, // IPv6 record
}

static async Task Main(string[] args)
{
    var result = await GetHostAddressAsync("www.bing.com").ConfigureAwait(false);
    Console.WriteLine($"Result: {result}");
    Console.ReadKey();
}

static async Task<Tuple<string, uint>> GetHostAddressAsync(string hostname, DnsRecordTypes type = DnsRecordTypes.DNS_TYPE_A)
{
    var taskWrapper = new TaskCompletionSource<Tuple<string, uint>>();
    var callback = new CallbackDelegate(
        (status, ipAddress, ttl) =>
        {
            if (status != 0)
            {
                taskWrapper.SetException(new Exception($"DNS query for {hostname} failed. ErrorCode: {status}"));
                return;
            }
            
            taskWrapper.SetResult(Tuple.Create(ipAddress, ttl));
        });

    ResolveDnsName(hostname, type, callback);
    return await taskWrapper.Task;
}

/// <summary>
/// Resolves the given DNS name using native WIN32 DnsQueryEx method.
/// </summary>
/// <param name="hostname">DNS name to resolve.</param>
/// <param name="type">Type of record to retrieve.</param>
/// <param name="callback">Function to callback when resolution is complete.</param>
/// <remarks>
/// You can get the entry point by using VS Command Prompt.
/// `dumpbin /exports DnsResolver.dll`.
/// For x86 use EntryPoint = @"_ResolveDnsName".
/// </remarks>
[DllImport("DnsNameResolver.dll", EntryPoint = @"ResolveDnsName", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
internal static extern void ResolveDnsName(string hostname, DnsRecordTypes type, CallbackDelegate callback);
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 807 2/27/2020
1.0.0 569 2/26/2020