如何在 IIS 下使用 HTTP 处理程序处理长时间运行的请求?
我需要在 IIS 内处理长时间运行的请求,处理请求本身非常轻量级,但主要由于 IO 而花费大量时间。基本上我需要查询另一台服务器,有时它会查询第三台服务器。所以我想同时处理尽可能多的请求。为此,我需要异步处理请求,如何正确执行?
使用 Socket 类,我可以轻松地编写如下内容:
// ..listening code
// ..Accepting code
void CalledOnRecive(Request request)
{
//process the request a little
Context context = new Context()
context.Socket = request.Socket;
remoteServer.Begin_LongRunningDemonicMethod(request.someParameter, DoneCallBack, context);
}
void DoneCallBack( )
{
IAsyncresult result = remoteServer.Begin_CallSomeVeryLongRunningDemonicMethod( request.someParameter, DoneCallBack, context);
Socket socket = result.Context.Socket;
socket.Send(result.Result);
}
在上面的示例中,一旦我调用“Begin...”方法,线程就会被释放,并且响应会在另一个线程上发送,因此您可以轻松实现非常高的并发性。 如何在 IIS 内的 HTTP 处理程序中执行相同的操作?
I need to process long running requests inside IIS, handling the request itself is very lightweight but takes a lot of time mostly due to IO. Basically I need to query another server, which sometimes queries a third server. So I want to process as many requests as I can simultaneously. For that I need to process the requests asynchronously how do I do that correctly?
Using the Socket class I could easily write something like :
// ..listening code
// ..Accepting code
void CalledOnRecive(Request request)
{
//process the request a little
Context context = new Context()
context.Socket = request.Socket;
remoteServer.Begin_LongRunningDemonicMethod(request.someParameter, DoneCallBack, context);
}
void DoneCallBack( )
{
IAsyncresult result = remoteServer.Begin_CallSomeVeryLongRunningDemonicMethod( request.someParameter, DoneCallBack, context);
Socket socket = result.Context.Socket;
socket.Send(result.Result);
}
In the example above the thread is released as soon as I call the "Begin..." method, and the response is sent on another thread, so you can easily achieve very high concurrency .
How do you do the same in an HTTP handler inside IIS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
IHttpAsyncHandler
< 实现 HttpHandler /a>. MSDN 有一个很好的演练,其中包含有关如何执行此操作的示例此处。You can implement an HttpHandler with
IHttpAsyncHandler
. MSDN has a nice walkthrough with examples on how to do that here.从这样的事情开始:
如果您需要将多个请求链接在一起,您可以在异步
HttpHandler
中执行此操作,但如果使用异步Page
会更容易。Start with something like this:
If you need to chain multiple requests together, you can do so in an async
HttpHandler
, but it's easier if you use an asyncPage
.