【问题标题】:How to process long running requests with an HTTP handler under IIS?如何在 IIS 下使用 HTTP 处理程序处理长时间运行的请求?
【发布时间】:2012-02-01 21:37:06
【问题描述】:

我需要在 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 处理程序中做同样的事情?

【问题讨论】:

    标签: c# asp.net .net iis httphandler


    【解决方案1】:

    您可以使用IHttpAsyncHandler 实现一个HttpHandler。 MSDN 有一个很好的演练,其中包含有关如何做到这一点的示例here

    【讨论】:

      【解决方案2】:

      从这样的开始:

      public class Handler : IHttpAsyncHandler {
          public void ProcessRequest(HttpContext context)
          {
          }
      
          public IAsyncResult BeginProcessRequest(HttpContext context,
                  AsyncCallback cb, object extraData)
          {
              IAsyncResult ar = BeginYourLongAsyncProcessHere(cb);
              return ar;
          }
      
          public void EndProcessRequest(IAsyncResult ar)
          {
              Object result = EndYourLongAsyncProcessHere(ar);
          }
      
          public bool IsReusable { get { return false; } }
      }
      

      如果您需要将多个请求链接在一起,您可以在 async HttpHandler 中执行此操作,但如果您使用 async Page 会更容易。

      【讨论】:

        猜你喜欢
        • 2012-08-20
        • 2020-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-12
        • 1970-01-01
        相关资源
        最近更新 更多