Unity2.0をIoCコンテナーとして使用するASP.NetMVC3Webアプリケーションを開発しています。
以下に、 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コンテナの各インスタンスを破棄する方法を教えてもらえますか?
ありがとう。