我正在开发一个使用 Unity 2.0 作为 IoC 容器的 ASP.Net MVC 3 Web 应用程序。
下面显示了我的Global.asax文件中的Application_Start()方法的示例
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
IUnityContainer container = new UnityContainer();
container.RegisterType<IControllerActivator, CustomControllerActivator>(
new HttpContextLifetimeManager<IControllerActivator>());
//container.RegisterType<IUnitOfWork, UnitOfWork>(
// new ContainerControlledLifetimeManager());
container.RegisterType<IUnitOfWork, UnitOfWork>(
new HttpContextLifetimeManager<IUnitOfWork>());
container.RegisterType<IListService, ListService>(
new HttpContextLifetimeManager<IListService>());
container.RegisterType<IShiftService, ShiftService>(
new HttpContextLifetimeManager<IShiftService>());
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
我的HttpContextLifetimeManager看起来像这样
public class HttpContextLifetimeManager<T> : LifetimeManager, IDisposable
{
public override object GetValue()
{
return HttpContext.Current.Items[typeof(T).AssemblyQualifiedName];
}
public override void RemoveValue()
{
HttpContext.Current.Items.Remove(typeof(T).AssemblyQualifiedName);
}
public override void SetValue(object newValue)
{
HttpContext.Current.Items[typeof(T).AssemblyQualifiedName] =
newValue;
}
public void Dispose()
{
RemoveValue();
}
}
我的问题是,当我在上面设置断点时,永远不会调用上述类中的方法Dispose() 。我担心我的 IoC 容器实例永远不会被处理掉。这会导致问题吗?
我找到了我放在Global.asax文件中的这段代码,但仍然没有调用Dispose()方法
protected void Application_EndRequest(object sender, EventArgs e)
{
using (DependencyResolver.Current as IDisposable);
}
谁能帮助我处理我的 Unity 容器的每个实例?
谢谢。