HttpListener.BeginGetContext(AsyncCallback, Object) Yöntem

Tanım

Gelen isteği zaman uyumsuz olarak almaya başlar.

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

Parametreler

callback
AsyncCallback

İstemci AsyncCallback isteği kullanılabilir olduğunda çağırma yöntemine başvuran bir temsilci.

state
Object

İşlem hakkında bilgi içeren kullanıcı tanımlı bir nesne. İşlem tamamlandığında bu nesne temsilciye callback geçirilir.

Döndürülenler

IAsyncResult Zaman uyumsuz işlemin durumunu gösteren nesne.

Özel durumlar

Win32 işlev çağrısı başarısız oldu. Özel durumun nedenini belirlemek için özel durumun ErrorCode özelliğini denetleyin.

Bu nesne başlatılmadı veya şu anda durduruldu.

Bu nesne kapatıldı.

Örnekler

Aşağıdaki kod örneği, gelen istemci isteklerini işleyecek bir geri çağırma yöntemi belirtmek için yönteminin kullanılmasını BeginGetContext gösterir.


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

Aşağıdaki kod örneği bir geri çağırma yöntemi uygular.

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

Açıklamalar

yöntemi, BeginGetContext gelen istemci isteklerini almak için zaman uyumsuz (engelleyici olmayan) bir çağrı başlatır. Bu yöntemi çağırmadan önce yöntemini çağırmanız Start ve özelliği tarafından Prefixes döndürülen URI dizelerini HttpListenerPrefixCollection ekleyerek dinlemek için en az bir Tekdüzen Kaynak Tanımlayıcısı (URI) ön eki eklemeniz gerekir.

Zaman uyumsuz işlem yöntemi çağrılarak EndGetContext tamamlanmalıdır. Genellikle, yöntemi temsilci tarafından çağrılır callback .

İşlem tamamlarken bu yöntem engellemez. Gelen bir istek almak ve işlem tamamlanana kadar engellemek için yöntemini çağırın GetContext .

Zaman uyumsuz programlama modelini kullanma hakkında ayrıntılı bilgi için bkz. Zaman Uyumlu Yöntemleri Zaman Uyumsuz Olarak Çağırma.

Arayanlara Notlar

Uygulamanızda ağ izlemeyi etkinleştirdiğinizde, bu üye izleme bilgilerini çıkarır. Daha fazla bilgi için .NET Framework ağ izleme bölümüne bakın.

Şunlara uygulanır