MVC3 IControllerFactory Release控制器内存泄漏

本文关键字:内存 泄漏 控制器 Release IControllerFactory MVC3 | 更新日期: 2023-09-27 18:28:06

在我们的项目中,我们使用MefControllerFactory,这些代码是我们在global.asax中的application_start设置为ControllerBuilder.Current.SetControllerFactory(new Project.Helper.MefControllerFactory(container)); 的代码

问题是,当我们运行.NET Profiler时,即使我们在"ReleaseController"方法中处理控制器,也会出现内存泄漏问题。原始消息为"已释放实例,但仍可从一个或多个根访问。由于已释放实例通常不应再使用,这可能表示内存泄漏。请调查该实例,以查明该实例是否意外保持活动状态,或者是否可以忽略此问题。"这个问题有什么解决办法吗?

第二条消息是:"已处置具有直接EventHandler根的实例已处理的实例由EventHandler直接作为根,即该实例仅用作EventHandler的目标,并且在不通过委托的情况下无法从任何其他根访问该实例。由于已释放的实例不应再使用,而EventHandlers是内存泄漏的常见原因,因此此问题强烈表明内存泄漏。"

public class MefControllerFactory : IControllerFactory
{
    private CompositionContainer _container;
    private DefaultControllerFactory defaultControllerFactory;
    public MefControllerFactory(CompositionContainer container)
    {
        _container = container;
        this.defaultControllerFactory = new DefaultControllerFactory();
    }
    #region IControllerFactory Members
    public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
    {
        IController controller = null;
        controller = _container.GetExportedValueOrDefault<IController>(controllerName);
        if (controller == null)
            return this.defaultControllerFactory.CreateController(requestContext, controllerName);
        return controller;
    }
    public void ReleaseController(IController controller)
    {
        IDisposable disposable = controller as IDisposable;
        if (disposable != null)
            disposable.Dispose();
    }
    #endregion

    public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName)
    {
        return System.Web.SessionState.SessionStateBehavior.Default;
    }
}

谢谢。。。

MVC3 IControllerFactory Release控制器内存泄漏

这是我为我的书的代码示例创建的基于MEF的控制器工厂:

public class MefControllerFactory : DefaultControllerFactory
{
    private readonly CompositionContainer container;
    private readonly Dictionary<IController, Lazy<object, object>> exports;
    private readonly object syncRoot;
    public MefControllerFactory(CompositionContainer container)
    {
        if (container == null)
        {
            throw new ArgumentNullException("container");
        }
        this.container = container;
        this.exports = new Dictionary<IController, Lazy<object, object>>();
        this.syncRoot = new object();
    }
    protected override IController GetControllerInstance(
        RequestContext requestContext, Type controllerType)
    {
        var export = this.container.GetExports(
            controllerType, null, null).Single();
        var controller = (IController)export.Value;
        lock (this.syncRoot)
        {
            this.exports.Add(controller, export);
        }
        return controller;
    }
    public override void ReleaseController(IController controller)
    {
        lock (this.syncRoot)
        {
            var export = this.exports[controller];
            this.exports.Remove(controller);
            this.container.ReleaseExport(export);
        }
        base.ReleaseController(controller);
    }
}

在MEF中,只能通过释放导出来释放对象图,而不能释放导出的