HttpListener.BeginGetContext(AsyncCallback, Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
开始异步检索传入的请求。
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 方法,并将至少一个统一资源标识符 (URI 添加) 前缀来侦听,方法是将 URI 字符串添加到 HttpListenerPrefixCollection 属性返回的 Prefixes 。
异步操作必须通过调用 EndGetContext 方法来完成。 通常,委托会调用 callback
方法。
此方法不会在操作完成时阻止。 若要获取传入请求并阻止操作完成,请 GetContext 调用 方法。
有关使用异步编程模型的详细信息,请参阅 异步调用同步方法。
调用方说明
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪。