STA MVC应用程序不会结束长时间运行的请求

时间:2017-11-02 13:41:31

标签: c# asp.net asp.net-mvc iis com

我需要在STA模式下运行我的ASP.NET MVC应用程序。为此,我开发了一些基于Programming ASP.NET MVC 4: Developing Real-World Web Applications with ASP.NET MVC的自定义类。他们在这里:

public class StaThreadRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        if (requestContext == null)
            throw new ArgumentNullException("requestContext");

        return new StaThreadHttpAsyncHandler(requestContext);
    }
}

public class StaThreadHttpAsyncHandler : Page, IHttpAsyncHandler, IRequiresSessionState
{
    private readonly RequestContext _requestContext;
    public StaThreadHttpAsyncHandler(RequestContext requestContext)
    {
        if (requestContext == null)
            throw new ArgumentNullException("requestContext");

        _requestContext = requestContext;
    }

    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
    {
        return this.AspCompatBeginProcessRequest(context, cb, extraData);
    }

    protected override void OnInit(EventArgs e)
    {
        var controllerName = _requestContext.RouteData.GetRequiredString("controller");
        var controllerFactory = ControllerBuilder.Current.GetControllerFactory();
        var controller = controllerFactory.CreateController(_requestContext, controllerName);
        if (controller == null)
            throw new InvalidOperationException("Could not find controller: " + controllerName);
        try
        {
            controller.Execute(_requestContext);
        }
        finally
        {
            controllerFactory.ReleaseController(controller);
        }
        this.Context.ApplicationInstance.CompleteRequest();
    }

    public void EndProcessRequest(IAsyncResult result)
    {
        this.AspCompatEndProcessRequest(result);
    }

    public override void ProcessRequest(HttpContext httpContext)
    {
        throw new NotSupportedException("STAThreadRouteHandler does not support ProcessRequest called (only BeginProcessRequest)");
    }
}

还调整了RouteConfig:

routes.Add(new Route("{controller}/{action}/{id}", new StaThreadRouteHandler ()))

对于我的大多数动作来说,一切都很好,但我有两个需要10到20秒才能完成。对于这两个操作,方法EndProcessRequest永远不会被执行,即使在执行期间没有抛出异常并且在this.Context.ApplicationInstance.CompleteRequest();内调用OnInit也没有问题。因此,此类请求在状态为ExecuteRequestHandler的IIS中结束,并且永久停留在那里,并且还阻止了新请求的服务。 enter image description here

为什么会这样?为什么EndProcessRequest中的断点永远不会被长时间运行的动作击中,而它可以用于较短的动作(1-5秒)。

1 个答案:

答案 0 :(得分:0)

问题与特定行动的执行时间无关。不确定引擎盖下发生了什么,但所有有问题的动作都有一个共同点 - 它们正在创建一个特定的COM对象,我猜这个对象在某种程度上没有被正确销毁,结果EndProcessRequest没有被调用。

任何.NET COM对象都会出现此问题,原生COM对象很好,并且会调用EndProcessRequest