Export (0) Print
Expand All

Dns.BeginResolve Method

Begins an asynchronous request to resolve a DNS host name or IP address to an IPAddress instance.

[Visual Basic]
Public Shared Function BeginResolve( _
   ByVal hostName As String, _
   ByVal requestCallback As AsyncCallback, _
   ByVal stateObject As Object _
) As IAsyncResult
[C#]
public static IAsyncResult BeginResolve(
 string hostName,
 AsyncCallback requestCallback,
 object stateObject
);
[C++]
public: static IAsyncResult* BeginResolve(
 String* hostName,
 AsyncCallback* requestCallback,
 Object* stateObject
);
[JScript]
public static function BeginResolve(
   hostName : String,
 requestCallback : AsyncCallback,
 stateObject : Object
) : IAsyncResult;

Parameters

hostName
A string containing the DNS name of the host.
requestCallback
The AsyncCallback.
stateObject
The State object.

Return Value

An IAsyncResult instance that references the asynchronous request.

Exceptions

Exception Type Condition
ArgumentNullException hostName is a null reference (Nothing in Visual Basic).
SocketException The caller does not have permission to access DNS information.
SecurityException An error occurred when executing the DNS query.

Remarks

The BeginResolve method starts an asynchronous request for DNS host information. The asynchronous callback method uses the EndResolve method to return the actual host information.

Example

[Visual Basic, C#, C++] The following example uses BeginResolve to resolve a DNS host name to an IPAddress.

[Visual Basic] 
Class DnsBeginGetHostByName
    
    Class RequestState
        Public host As IPHostEntry
        
        Public Sub New()
            host = Nothing
        End Sub 'New
    End Class 'RequestState
    
    
    Public Shared Sub Main()
     Try
        ' Create an instance of the RequestState class.
        Dim myRequestState As New RequestState()
        
        ' Begin an asynchronous request for information such as the host name, IP addresses, 
        ' or aliases for the specified URI.
        Dim asyncResult As IAsyncResult = CType(Dns.BeginResolve("www.contoso.com", AddressOf RespCallback, myRequestState),IAsyncResult)

        ' Wait until asynchronous call completes.
        While asyncResult.IsCompleted <> True
        End While
        
        Console.WriteLine(("Host name : " + myRequestState.host.HostName))
        Console.WriteLine(ControlChars.Cr + "IP address list : ")
        Dim index As Integer
        For index = 0 To myRequestState.host.AddressList.Length - 1
            Console.WriteLine(myRequestState.host.AddressList(index))
        Next index
        Console.WriteLine(ControlChars.Cr + "Aliases : ")

        For index = 0 To myRequestState.host.Aliases.Length - 1
            Console.WriteLine(myRequestState.host.Aliases(index))
        Next index
    catch e as Exception
            Console.WriteLine("Exception caught!!!")
            Console.WriteLine(("Source : " + e.Source))
            Console.WriteLine(("Message : " + e.Message))
    End Try
    End Sub 'Main
    
    
    Private Shared Sub RespCallback(ar As IAsyncResult)
        Try
            ' Convert the IAsyncResult object to a RequestState object.
            Dim tempRequestState As RequestState = CType(ar.AsyncState, RequestState)

            ' End the asynchronous request.
            tempRequestState.host = Dns.EndResolve(ar)
            Catch e As ArgumentNullException
            Console.WriteLine("ArgumentNullException caught!!!")
            Console.WriteLine(("Source : " + e.Source))
            Console.WriteLine(("Message : " + e.Message))
            Catch e As Exception
            Console.WriteLine("Exception caught!!!")
            Console.WriteLine(("Source : " + e.Source))
            Console.WriteLine(("Message : " + e.Message))
        End Try
    End Sub 'RespCallback

[C#] 
class DnsBeginGetHostByName
{   
   public static System.Threading.ManualResetEvent allDone = null;

   class RequestState
   {
      public IPHostEntry host;
      public RequestState()
      {
         host = null;
      }
   }

   public static void Main()
   {
     allDone = new ManualResetEvent(false);
     // Create an instance of the RequestState class.
     RequestState myRequestState = new RequestState();

     // Begin an asynchronous request for information like host name, IP addresses, or 
     // aliases for specified the specified URI.
     IAsyncResult asyncResult = Dns.BeginResolve("www.contoso.com", new AsyncCallback(RespCallback), myRequestState );

     // Wait until asynchronous call completes.
     allDone.WaitOne();
     Console.WriteLine("Host name : " + myRequestState.host.HostName);
     Console.WriteLine("\nIP address list : ");
     for(int index=0; index < myRequestState.host.AddressList.Length; index++)
     {
       Console.WriteLine(myRequestState.host.AddressList[index]);
     }      
     Console.WriteLine("\nAliases : ");
     for(int index=0; index < myRequestState.host.Aliases.Length; index++)
     {
       Console.WriteLine(myRequestState.host.Aliases[index]);
     }      
   }
   
   private static void RespCallback(IAsyncResult ar)
   {
     try 
     {
       // Convert the IAsyncResult object to a RequestState object.
       RequestState tempRequestState = (RequestState)ar.AsyncState;
       // End the asynchronous request.
       tempRequestState.host = Dns.EndResolve(ar);
       allDone.Set();
     }
       catch(ArgumentNullException e) 
     {
       Console.WriteLine("ArgumentNullException caught!!!");
       Console.WriteLine("Source : " + e.Source);
       Console.WriteLine("Message : " + e.Message);
     }   
     catch(Exception e)
     {
       Console.WriteLine("Exception caught!!!");
       Console.WriteLine("Source : " + e.Source);
       Console.WriteLine("Message : " + e.Message);
     }
   }
}

[C++] 
public __gc class DnsBeginGetHostByName
{
public:
   static System::Threading::ManualResetEvent* allDone = 0;
   __gc class RequestState
   {
   public:
      IPHostEntry*  host;
      RequestState()
      {
         host = 0;
      }
   };

   static void RespCallback(IAsyncResult* ar)
   {
      try
      {
         // Convert the IAsyncResult* Object* to a RequestState Object*.
         RequestState* tempRequestState = 
            dynamic_cast<RequestState*>(ar->AsyncState);
         // End the asynchronous request.
         tempRequestState->host = Dns::EndResolve(ar);
         allDone->Set();
      }
      catch (ArgumentNullException* e)
      {
         Console::WriteLine(S"ArgumentNullException caught!!!");
         Console::WriteLine(S"Source : {0}", e->Source);
         Console::WriteLine(S"Message : {0}", e->Message);
      }
      catch (Exception* e)
      {
         Console::WriteLine(S"Exception caught!!!");
         Console::WriteLine(S"Source : {0}", e->Source);
         Console::WriteLine(S"Message : {0}", e->Message);
      }
   }
};

int main()
{
   DnsBeginGetHostByName::allDone = new ManualResetEvent(false);
   // Create an instance of the RequestState class.
   DnsBeginGetHostByName::RequestState* myRequestState = 
      new DnsBeginGetHostByName::RequestState();

   // Begin an asynchronous request for information like host name, IP addresses, or
   // aliases for specified the specified URI.
   IAsyncResult* asyncResult = Dns::BeginResolve(S"www.contoso.com", 
      new AsyncCallback(asyncResult, 
      DnsBeginGetHostByName::RespCallback), 
      myRequestState);

   // Wait until asynchronous call completes.
   DnsBeginGetHostByName::allDone->WaitOne();
   Console::WriteLine(S"Host name : {0}", myRequestState->host->HostName);
   Console::WriteLine(S"\nIP address list : ");
   for (int index=0; index < myRequestState->host->AddressList->Length; index++)
      Console::WriteLine(myRequestState->host->AddressList->Item[index]);
   Console::WriteLine(S"\nAliases : ");
   for (int index=0; index < myRequestState->host->Aliases->Length; index++)
      Console::WriteLine(myRequestState->host->Aliases->Item[index]);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Dns Class | Dns Members | System.Net Namespace | Including Asynchronous Calls

Show:
© 2015 Microsoft