ASP MVC:何时调用 IController Dispose()? [英] ASP MVC: When is IController Dispose() called?

查看:18
本文介绍了ASP MVC:何时调用 IController Dispose()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对我的一个较大的 MVC 应用程序进行大规模重构/速度调整.它已经部署到生产中几个月了,我开始在连接池中等待连接超时.我已将问题追溯到未正确处理的连接.

I'm going through a big refactoring / speed tweaking of one of my larger MVC apps. It has been deployed to production for a few months now, and I was starting to get timeouts waiting for connections in the connection pool. I have tracked the issue down to the connections not getting disposed properly.

有鉴于此,我已经对我的基本控制器进行了更改:

In light of that, I have since made this change to my base controller:

public class MyBaseController : Controller
{
    private ConfigurationManager configManager;  // Manages the data context.

    public MyBaseController()
    {
         configManager = new ConfigurationManager();
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (this.configManager != null)
            {
                this.configManager.Dispose();
                this.configManager = null;
            }
        }

        base.Dispose(disposing);
    }
}

现在,我有两个问题:

  1. 我是否引入了竞争条件?由于 configManager 管理公开 IQueryable<>DataContext参数视图,我需要确保 Dispose() 不会被调用在视图完成渲染之前在控制器上.
  2. MVC 框架是在渲染视图之前还是之后调用控制器上的 Dispose()?或者,MVC 框架是否保留了这一点直到 GarbageCollector?
  1. Am I introducing a race condition? Since the configManager manages the DataContext that exposes IQueryable<> parameters to the views, I need to make sure that Dispose() will not be called on the controller before the view finishes rendering.
  2. Does the MVC framework call Dispose() on the Controller before or after the view is rendered? Or, does the MVC framework leave that up to the GarbageCollector?

推荐答案

在视图呈现后调用 Dispose,总是.

Dispose is called after the view is rendered, always.

视图在对 ActionResult.ExecuteResult 的调用中呈现.这被 ControllerActionInvoker.InvokeAction(间接)调用,而后者又被 ControllerBase.ExecuteCore 调用.

The view is rendered in the call to ActionResult.ExecuteResult. That's called (indirectly) by ControllerActionInvoker.InvokeAction, which is in turn called by ControllerBase.ExecuteCore.

由于在渲染视图时控制器在调用堆栈中,因此无法在那时处理它.

Since the controller is in the call stack when the view is rendered, it cannot be disposed then.

这篇关于ASP MVC:何时调用 IController Dispose()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆