HttpListener.BeginGetContext(AsyncCallback, Object) メソッド

定義

受信要求の非同期の取得を開始します。

public:
 IAsyncResult ^ BeginGetContext(AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginGetContext (AsyncCallback? callback, object? state);
public IAsyncResult BeginGetContext (AsyncCallback callback, object state);
member this.BeginGetContext : AsyncCallback * obj -> IAsyncResult
Public Function BeginGetContext (callback As AsyncCallback, state As Object) As IAsyncResult

パラメーター

callback
AsyncCallback

クライアント要求が使用可能なときに呼び出すメソッドを参照する AsyncCallback デリゲート。

state
Object

操作に関する情報を格納するユーザー定義のオブジェクト。 このオブジェクトは、操作の完了時に callback デリゲートに渡されます。

戻り値

非同期操作のステータスを示す IAsyncResult オブジェクト。

例外

Win32 関数呼び出しが失敗しました。 例外の ErrorCode プロパティを調べて、例外の原因を確認します。

このオブジェクトが開始されていないか、現在停止されています。

オブジェクトが閉じています。

次のコード例は、 メソッドを BeginGetContext 使用して、受信クライアント要求を処理するコールバック メソッドを指定する方法を示しています。


public static void NonblockingListener(string [] prefixes)
{
    HttpListener listener = new HttpListener();
    foreach (string s in prefixes)
    {
        listener.Prefixes.Add(s);
    }
    listener.Start();
    IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback),listener);
    // Applications can do some work here while waiting for the
    // request. If no work can be done until you have processed a request,
    // use a wait handle to prevent this thread from terminating
    // while the asynchronous operation completes.
    Console.WriteLine("Waiting for request to be processed asyncronously.");
    result.AsyncWaitHandle.WaitOne();
    Console.WriteLine("Request processed asyncronously.");
    listener.Close();
}
Public Shared Sub NonblockingListener(ByVal prefixes As String())
    Dim listener As HttpListener = New HttpListener()

    For Each s As String In prefixes
        listener.Prefixes.Add(s)
    Next

    listener.Start()
    Dim result As IAsyncResult = listener.BeginGetContext(New AsyncCallback(AddressOf ListenerCallback), listener)
    ' Applications can do some work here while waiting for the 
    ' request. If no work can be done until you have processed a request,
    ' use a wait handle to prevent this thread from terminating
    ' while the asynchronous operation completes.
    Console.WriteLine("Waiting for request to be processed asyncronously.")
    result.AsyncWaitHandle.WaitOne()
    Console.WriteLine("Request processed asyncronously.")
    listener.Close()
End Sub

次のコード例では、コールバック メソッドを実装しています。

public static void ListenerCallback(IAsyncResult result)
{
    HttpListener listener = (HttpListener) result.AsyncState;
    // Call EndGetContext to complete the asynchronous operation.
    HttpListenerContext context = listener.EndGetContext(result);
    HttpListenerRequest request = context.Request;
    // Obtain a response object.
    HttpListenerResponse response = context.Response;
    // Construct a response.
    string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    // Get a response stream and write the response to it.
    response.ContentLength64 = buffer.Length;
    System.IO.Stream output = response.OutputStream;
    output.Write(buffer,0,buffer.Length);
    // You must close the output stream.
    output.Close();
}
Public Shared Sub ListenerCallback(ByVal result As IAsyncResult)
    Dim listener As HttpListener = CType(result.AsyncState, HttpListener)
    ' Call EndGetContext to complete the asynchronous operation.
    Dim context As HttpListenerContext = listener.EndGetContext(result)
    Dim request As HttpListenerRequest = context.Request
    ' Obtain a response object.
    Dim response As HttpListenerResponse = context.Response
    ' Construct a response.
    Dim responseString As String = "<HTML><BODY> Hello world!</BODY></HTML>"
    Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(responseString)
    ' Get a response stream and write the response to it.
    response.ContentLength64 = buffer.Length
    Dim output As System.IO.Stream = response.OutputStream
    output.Write(buffer, 0, buffer.Length)
    ' You must close the output stream.
    output.Close()
End Sub

注釈

メソッドは BeginGetContext 、着信クライアント要求を受信するために非同期 (非ブロッキング) 呼び出しを開始します。 このメソッドを呼び出す前に、 メソッドをStart呼び出し、 プロパティによってPrefixes返される に URI 文字列を追加することでリッスンする URI (Uniform Resource Identifier) プレフィックスを少なくとも 1 つ追加するHttpListenerPrefixCollection必要があります。

非同期操作は、 メソッドを呼び出して完了する EndGetContext 必要があります。 通常、 メソッドはデリゲートによって callback 呼び出されます。

このメソッドは、操作の完了中はブロックしません。 受信要求を取得し、操作が完了するまでブロックするには、 メソッドを GetContext 呼び出します。

非同期プログラミング モデルの使用の詳細については、「同期メソッドの非同期呼び出し」を参照してください。

注意 (呼び出し元)

このメンバーは、アプリケーションでネットワーク トレースが有効にされている場合にトレース情報を出力します。 詳細については、「.NET Frameworkのネットワーク トレース」を参照してください。

適用対象