Dns.BeginResolve(String, AsyncCallback, Object) 方法

定義

警告

BeginResolve is obsoleted for this type, please use BeginGetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202

警告

BeginResolve has been deprecated. Use BeginGetHostEntry instead.

警告

BeginResolve is obsoleted for this type, please use BeginGetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202

開始非同步要求,將 DNS 主機名稱或 IP 位址解析為 IPAddress 執行個體。

C#
[System.Obsolete("BeginResolve is obsoleted for this type, please use BeginGetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202")]
public static IAsyncResult BeginResolve(string hostName, AsyncCallback? requestCallback, object? stateObject);
C#
[System.Obsolete("BeginResolve has been deprecated. Use BeginGetHostEntry instead.")]
public static IAsyncResult BeginResolve(string hostName, AsyncCallback? requestCallback, object? stateObject);
C#
[System.Obsolete("BeginResolve is obsoleted for this type, please use BeginGetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202")]
public static IAsyncResult BeginResolve(string hostName, AsyncCallback requestCallback, object stateObject);
C#
[System.Obsolete("BeginResolve is obsoleted for this type, please use BeginGetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202")]
public static IAsyncResult BeginResolve(string hostName, AsyncCallback requestCallback, object stateObject);
C#
public static IAsyncResult BeginResolve(string hostName, AsyncCallback requestCallback, object stateObject);

參數

hostName
String

主機的 DNS 名稱。

requestCallback
AsyncCallback

AsyncCallback 委派,會於作業完成時參考要叫用的方法。

stateObject
Object

使用者定義物件,包含作業的相關資訊。 作業完成時會將這個物件傳遞至 requestCallback 委派。

傳回

參考非同步要求的 IAsyncResult 執行個體。

屬性

例外狀況

hostNamenull

呼叫端並沒有可以存取 DNS 資訊的使用權限。

範例

下列範例會使用 BeginResolve 將 DNS 主機名解析為 IPAddress

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);
     }
   }
}

備註

異步 BeginResolve 操作必須藉由呼叫 EndResolve 方法來完成。 一般而言,委派會叫 requestCallback 用 方法。

在作業完成之前,此方法不會封鎖。 若要封鎖直到作業完成為止,請使用 Resolve 方法。

如需使用異步程序設計模型的詳細資訊,請參閱 以異步方式呼叫同步方法

備註

當您在應用程式中啟用網路追蹤時,此成員會發出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤

適用於

產品 版本 (已過時)
.NET (Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9)
.NET Framework 1.1 (2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1)
.NET Standard (2.0, 2.1)

另請參閱