ASP MVC:当一个IController的Dispose()叫什么名字? [英] ASP MVC: When is IController Dispose() called?

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

问题描述

我要通过我的大MVC应用程序中的一个大的重构/速度调整。它已被部署到生产几个月,现在,我开始变得超时等待连接池中的连接。我已经跟踪这个问题到的连接没有得到妥善处理。

在那光,因为我已经做出这个改变我的基本控制器:

 公共类MyBaseController:控制器
{
    私人ConfigurationManager中CONFIGMANAGER; //管理数据上下文。    公共MyBaseController()
    {
         CONFIGMANAGER =新ConfigurationManager中();
    }    保护覆盖无效的Dispose(BOOL处置)
    {
        如果(处置)
        {
            如果(this.configManager!= NULL)
            {
                this.configManager.Dispose();
                this.configManager = NULL;
            }
        }        base.Dispose(处置);
    }
}

现在,我有两个问题:


  1. 我是引入竞争条件?由于 CONFIGMANAGER 管理​​的DataContext 的自曝的IQueryable<> 参数
    的意见,我需要确保的Dispose()将不会被调用
    视图之前控制器上渲染完毕。

  2. 是否MVC框架调用的Dispose()之前或视图渲染之后,控制器?或者说,MVC框架离开该
    到GarbageCollector?


解决方案

调用Dispose的观点被渲染后的总是

该视图在调用呈现给 ActionResult.ExecuteResult 。这就是所谓的(间接) ControllerActionInvoker.InvokeAction ,这又由 ControllerBase.ExecuteCore 调用。

由于控制器是在调用栈时,视图显示,它不能被再布置。

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);
    }
}

Now, I have two questions:

  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 is called after the view is rendered, always.

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天全站免登陆