Share via


HttpListener.BeginGetContext(AsyncCallback, Object) Metode

Definisi

Mulai mengambil permintaan masuk secara asinkron.

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

Parameter

callback
AsyncCallback

Delegasi AsyncCallback yang mereferensikan metode yang akan dipanggil saat permintaan klien tersedia.

state
Object

Objek yang ditentukan pengguna yang berisi informasi tentang operasi. Objek ini diteruskan ke callback delegasi ketika operasi selesai.

Mengembalikan

Objek IAsyncResult yang menunjukkan status operasi asinkron.

Pengecualian

Panggilan fungsi Win32 gagal. Periksa properti pengecualian ErrorCode untuk menentukan penyebab pengecualian.

Objek ini belum dimulai atau saat ini dihentikan.

Objek ini ditutup.

Contoh

Contoh kode berikut menunjukkan menggunakan BeginGetContext metode untuk menentukan metode panggilan balik yang akan menangani permintaan klien masuk.


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

Contoh kode berikut mengimplementasikan metode panggilan balik.

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

Keterangan

Metode ini BeginGetContext memulai panggilan asinkron (non-pemblokiran) untuk menerima permintaan klien masuk. Sebelum memanggil metode ini, Anda harus memanggil Start metode dan menambahkan setidaknya satu awalan Pengidentifikasi Sumber Daya Seragam (URI) untuk didengarkan dengan menambahkan string URI ke HttpListenerPrefixCollection yang dikembalikan oleh Prefixes properti .

Operasi asinkron harus diselesaikan dengan memanggil EndGetContext metode . Biasanya, metode ini dipanggil oleh callback delegasi.

Metode ini tidak memblokir saat operasi selesai. Untuk mendapatkan permintaan masuk dan memblokir hingga operasi selesai, panggil GetContext metode .

Untuk informasi mendetail tentang menggunakan model pemrograman asinkron, lihat Memanggil Metode Sinkron Secara Asinkron.

Catatan Bagi Pemanggil

Anggota ini mengeluarkan informasi pelacakan saat Anda mengaktifkan pelacakan jaringan di aplikasi Anda. Untuk informasi selengkapnya, lihat Pelacakan Jaringan di .NET Framework.

Berlaku untuk