gpt4 book ai didi

c# - MVC3 IControllerFactory ReleaseController内存泄漏

转载 作者:行者123 更新时间:2023-11-30 15:04:50 26 4
gpt4 key购买 nike

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

问题是当我们在结果中运行 .NET Profiler 时,即使我们在“ReleaseController”方法中处理 Controller ,它也会产生内存泄漏问题。原始消息是 “一个实例已被释放,但仍可从一个或多个根访问。由于通常不应再使用已释放的实例,这可能表示内存泄漏。调查该实例以查明该实例是否是无意的保持活力,或者可以忽略该问题。”这个问题有什么解决办法吗?

第二条信息是:“具有直接 EventHandler 根的处置实例处置的实例直接由 EventHandler 建立根,即该实例仅用作 EventHandler 的目标,并且如果不通过委托(delegate)就无法从任何其他根访问它。由于不应再使用已处置的实例,并且事件处理程序是内存泄漏的常见原因,因此此问题是内存泄漏的强烈迹象。”

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

谢谢...

最佳答案

这是我为 my book 的代码示例创建的基于 MEF 的 Controller 工厂:

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 中,您只能通过释放导出 来释放对象图 - 而不是导出的

关于c# - MVC3 IControllerFactory ReleaseController内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9721953/

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