gpt4 book ai didi

asp.net-mvc - ASP MVC : When is IController Dispose() called?

转载 作者:行者123 更新时间:2023-12-03 05:11:24 28 4
gpt4 key购买 nike

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

有鉴于此,我对我的基本 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管理DataContext暴露IQueryable<>参数为意见,我需要确保 Dispose()不会被称为在 View 完成渲染之前在 Controller 上。
  2. MVC框架是否调用Dispose()在渲染 View 之前或之后在 Controller 上?或者,MVC 框架是否留下了这一点?到垃圾收集器?

最佳答案

在渲染 View 后调用 Dispose,始终

View 在对 ActionResult.ExecuteResult 的调用中呈现。它由 ControllerActionInvoker.InvokeAction (间接)调用,而后者又由 ControllerBase.ExecuteCore 调用。

由于渲染 View 时 Controller 位于调用堆栈中,因此无法将其释放。

关于asp.net-mvc - ASP MVC : When is IController Dispose() called?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1380019/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com