HttpListener.BeginGetContext(AsyncCallback, Object) 方法
定义
开始异步检索传入的请求。Begins asynchronously retrieving an incoming request.
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 委托,引用客户端请求可用时要调用的方法。An AsyncCallback delegate that references the method to invoke when a client request is available.
- state
- Object
一个用户定义对象,其中包含操作的相关信息。A user-defined object that contains information about the operation. 操作完成时,此对象传递给 callback 委托。This object is passed to the callback delegate when the operation completes.
返回
一个指示异步操作状态的 IAsyncResult 对象。An IAsyncResult object that indicates the status of the asynchronous operation.
例外
Win32 函数调用失败。A Win32 function call failed. 检查异常的 ErrorCode 属性以确定导致异常的原因。Check the exception's ErrorCode property to determine the cause of the exception.
此对象尚未启动或当前已停止。This object has not been started or is currently stopped.
此对象已关闭。This object is closed.
示例
下面的代码示例演示如何使用 BeginGetContext 方法指定将处理传入客户端请求的回调方法。The following code example demonstrates using the BeginGetContext method to specify a callback method that will handle incoming client requests.
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
下面的代码示例实现了一个回调方法。The following code example implements a callback method.
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方法开始异步 (非阻塞) 调用以接收传入的客户端请求。The BeginGetContext method begins an asynchronous (non-blocking) call to receive incoming client requests. 在调用此方法之前,必须调用 Start 方法,并添加至少一个统一资源标识符 (uri) 前缀,以便通过将 uri 字符串添加到属性返回的来侦听 HttpListenerPrefixCollection Prefixes 。Before calling this method, you must call the Start method and add at least one Uniform Resource Identifier (URI) prefix to listen for by adding the URI strings to the HttpListenerPrefixCollection returned by the Prefixes property.
必须通过调用方法完成异步操作 EndGetContext 。The asynchronous operation must be completed by calling the EndGetContext method. 通常,方法由 callback 委托调用。Typically, the method is invoked by the callback delegate.
操作完成时,此方法不会被阻止。This method does not block while the operation completes. 若要获取传入请求并在操作完成之前一直阻止,请调用 GetContext 方法。To get an incoming request and block until the operation completes, call the GetContext method.
有关使用异步编程模型的详细信息,请参阅 以异步方式调用同步方法For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously
调用方说明
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪。For more information, see Network Tracing in the .NET Framework.