gpt4 book ai didi

c# - 如何获得当前的 CaSTLe Windsor 容器?

转载 作者:可可西里 更新时间:2023-11-01 08:04:24 28 4
gpt4 key购买 nike

我是 CaSTLe Winsor Noob。我有一个一团糟的 WebForm 项目。我正在尝试解决测试用户注册的依赖性。如何到达当前的 WindsorContainer?

IWindsorContainer container = ???;
IRegistrationLogic registrationLogic = container.Resolve<IRegistrationLogic>();
_registrationLogic.Register();

这是我的 Bootstrap :

public class WindsorConfigTask : ICastleBootstrapperTask
{

public void Execute()
{
Container.AddFacility<WcfFacility>();
Container.Register(
Component.For<IProcessMessageRequest>()
.ActAs(
new DefaultClientModel
{
Endpoint =
WcfEndpoint.ForContract<IProcessMessageRequest>().FromConfiguration("surveyClient2")
}
),
Component.For<ILocalMembershipService>()
.ActAs(
new DefaultClientModel
{
Endpoint =
WcfEndpoint.ForContract<ILocalMembershipService>().FromConfiguration(
"localMembershipClient")
})


);

Container.Register(Component.For<IRegistrationLogic>()
.ImplementedBy<RegistrationLogic>()
.LifeStyle.Is(LifeStyleType));
}

public IWindsorContainer Container { get; set; }


#region ICastleBootstrapperTask Members


public Castle.Core.LifestyleType LifeStyleType
{
get;
set;
}

#endregion
}

最佳答案

Windsor 中有用于此目的的界面。它叫做IContainerAccessor .实现它的最佳位置是 Global.asax 文件:

public class WebApplication : HttpApplication, IContainerAccessor {
static IWindsorContainer container;

public IWindsorContainer Container {
get { return container; }
}

protected void Application_Start() {
var bootstrapper = new WindsorConfigTask();
bootstrapper.Execute();
container = bootstrapper.Container;
}

protected void Application_End() {
container.Dispose();
}
}

在你的web form中的用法如下:

var containerAccessor = Context.ApplicationInstance as IContainerAccessor;
var container = containerAccessor.Container;

关于c# - 如何获得当前的 CaSTLe Windsor 容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21122502/

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