Asp.net mvc 4 解决后释放服务
我在项目中使用了温莎城堡。我想创建惰性解析服务和存储库。示例代码:Asp.net mvc 4 解决后释放服务,asp.net-mvc-4,castle-windsor,Asp.net Mvc 4,Castle Windsor,我在项目中使用了温莎城堡。我想创建惰性解析服务和存储库。示例代码: public IPersonService PersonService { get { return _windsorContainer.Resolve<IPersonService>(); } set { _windsorContainer.Resolve<IPersonService>(value); } } public ICityServi
public IPersonService PersonService
{
get { return _windsorContainer.Resolve<IPersonService>(); }
set { _windsorContainer.Resolve<IPersonService>(value); }
}
public ICityService CityService
{
get { return _windsorContainer.Resolve<ICityService>(); }
set { _windsorContainer.Resolve<ICityService>(value); }
}
public IPhoneService PhoneService
{
get { return _windsorContainer.Resolve<IPhoneService>(); }
set { _windsorContainer.Resolve<IPhoneService>(value); }
}
public IPersonService PersonService
{
获取{return _windsorContainer.Resolve();}
设置{U windsorContainer.Resolve(值);}
}
公共ICITY服务城市服务
{
获取{return _windsorContainer.Resolve();}
设置{U windsorContainer.Resolve(值);}
}
公共iPhone服务电话服务
{
获取{return _windsorContainer.Resolve();}
设置{U windsorContainer.Resolve(值);}
}
问题是:解决结束后,我需要怎么做,释放这些。
Tnx
编辑 我的屋顶课是:
public class PhonebookApplication
{
public WindsorContainer _windsorContainer;
private void InitializeWindsor()
{
_windsorContainer = new WindsorContainer();
_windsorContainer.Install(Configuration.FromXmlFile("Castle.config"));
}
public void Dispose()
{
if (_windsorContainer != null)
{
_windsorContainer.Dispose();
}
}
private static readonly Lazy<PhonebookApplication> s_Current = new Lazy<PhonebookApplication>(() => new PhonebookApplication(), true);
public static PhonebookApplication Current
{
get { return s_Current.Value; }
}
public void ApplicationStart()
{
InitializeWindsor();
}
//Service count increaseble as your have.
public IPersonService PersonService
{
get { return _windsorContainer.Resolve<IPersonService>(); }
set { _windsorContainer.Resolve<IPersonService>(value); }
}
public ICityService CityService
{
get { return _windsorContainer.Resolve<ICityService>(); }
set { _windsorContainer.Resolve<ICityService>(value); }
}
public IPhoneService PhoneService
{
get { return _windsorContainer.Resolve<IPhoneService>(); }
set { _windsorContainer.Resolve<IPhoneService>(value); }
}
}
公共类电话簿应用程序
{
公众WindsorContainer_WindsorContainer;
private void InitializeWindsor()
{
_windsorContainer=新windsorContainer();
_安装(Configuration.FromXmlFile(“Castle.config”);
}
公共空间处置()
{
如果(_windsorContainer!=null)
{
_windsorContainer.Dispose();
}
}
private static readonly Lazy s_Current=new Lazy(()=>new PhonebookApplication(),true);
公共静态电话簿应用程序当前
{
获取{返回s_Current.Value;}
}
公共void应用程序启动()
{
初始化windsor();
}
//服务计数可随您的需要而增加。
公共IPersonService个人服务
{
获取{return _windsorContainer.Resolve();}
设置{U windsorContainer.Resolve(值);}
}
公共ICITY服务城市服务
{
获取{return _windsorContainer.Resolve();}
设置{U windsorContainer.Resolve(值);}
}
公共iPhone服务电话服务
{
获取{return _windsorContainer.Resolve();}
设置{U windsorContainer.Resolve(值);}
}
}
}
如果我想在HomeController上调用一个方法,我使用的方法是
PhoneBookApplication.Current.PhoneService.GetAllList()
,然后我想在方法调用完成后调用release Current resolved service。如果我将该类构造函数注入所有工厂类,则roof类依赖于此,因此我不希望在任何地方使用ctor inject。不要这样做-您可以使用WindsorsTypedFactory
Facility来在运行时解析和发布组件
您只需创建一个接口:
public interface IPersonServiceFactory
{
IPersonService Create();
void Release(IPersonService svc);
}
然后注册AsFactory
container.Register(Component.For<IPersonServiceFactory>().AsFactory());
您需要确保已将typeTypedFactoryFacility
添加到引导代码中的容器中:
container.AddFacility<TypedFactoryFacility>();
谢谢你的回答,你的答案是我的部分答案,在这种情况下,如果我想运行一个方法,那么我依赖注入“IPersonServiceFactory personServiceFactory”,但我不想通过注入访问一个方法,我想通过一个静态屋顶类访问方法。屋顶、人事服务等等。。我怎样才能在没有喷射的情况下实现工厂。嗨,我能把工厂作为拦截器来实现吗?不知道你的拦截器是如何工作的,你必须考虑什么会触发拦截器释放组件。我想您可以确保您的所有接口都继承
IDisposable
,并在调用disposed方法时让拦截器释放组件。你有什么理由不想为工厂注资吗?您可以使用container.release()释放组件。问题在于如何使组件自动释放?您的组件是否注册为瞬态?您能提供实际应用程序的更多背景信息吗?问题就像您所说的:我想为每个调用后的方法开发自动发布。我使用了瞬态和单例。您不需要释放单例-它们在容器的整个生命周期内都有效,但是既然您使用的是MVC,为什么不为您的组件使用LifestylePerWebRequest
?当web请求结束时,Windsor将自动管理已解决的生活方式-也不确定不使用注入会获得什么
container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<IPersonServiceFactory>().AsFactory(x => x.SelectedWith<PersonServiceFactoryComponentSelector>()));
container.Register(Component.For<PersonServiceFactoryComponentSelector>());
public class DispatcherFactoryComponentSelector : DefaultTypedFactoryComponentSelector
{
protected override string GetComponentName(System.Reflection.MethodInfo method, object[] arguments)
{
if(arguments[0] is bool && (bool)arguments[0] == true)
// Return a certain component name
else
// Return a different component name
}
}