现在的位置: 首页 > 综合 > 正文

Unity.MVC3管理对象生命周期

2012年11月22日 ⁄ 综合 ⁄ 共 1125字 ⁄ 字号 评论关闭

Most IDependencyResolver implementations using Unity do not work with IDisposable and using them can lead to memory leaks and connection issues. Unity.Mvc3 is an assembly that includes a fuller implementation of IDependencyResolver that uses a child container per web request to ensure that IDisposable instances are disposed of at the end of each request. All of these complexities are taken care of by the assembly and integration into a project is as simple as adding a single line of code.

Background

ASP.NET MVC 3 has a new mechanism for integrating an IoC container into the MVC pipeline. It involves writing an implementation of IDependencyResolver and registering it using the staticDependencyResolver.SetResolver method. It is a trivial process to write a simple DependencyResolver for Unity, but the vast majority of versions that you will see do not handle IDisposable, leading to memory leaks in your applications when dealing with disposable components. Here is a typically naive implementation:

public class OverlySimpleUnityDependencyResolver : IDependencyResolver
{
    private readonly IUnityContainer _container;
 
    public OverlySimpleUnityDependencyResolver(IUnityContainer container)
    {
        _container = container;
    }
 
    public object GetService(Type serviceType)

抱歉!评论已关闭.