【发布时间】:2015-01-19 03:55:28
【问题描述】:
我正在开发一个 ASP.NET MVC 4 Web 应用程序。默认控制器工厂已按照建议 here 替换为 WindsorControllerFactory。这很有用,因为此应用程序中的控制器包含对几个服务的引用,这些服务是使用 Windsor 注入实例化的。每个服务都有一个代理来包装它。
因此,我们有以下情况:
- 在 Castle 中注册了两个组件(一个服务和一个代理)
- 其中一个被构造为另一个的依赖项
它看起来像:
// This URL can be resolved at application startup
container.Register(Component.For<ITestService>()
.UsingFactoryMethod(() => ServiceFactory.CreateService<ITestService>(Settings.Default.ConfigurationProviderUrl))
.Named(MainServiceComponent)
.LifeStyle.Transient);
// The URL for this service can be configured during runtime. If it is null or empty it should not be resolved
container.Register(Component.For<ITestService>()
.UsingFactoryMethod(() => ServiceFactory.CreateService<ITestService>(SiteInformation.PublishUrl))
.Named(PublicationServiceComponent)
.LifeStyle.Transient);
// This proxy is necessary
container.Register(Component.For<IConfigurationProxy>()
.ImplementedBy<ConfigurationProxyWebService>()
.ServiceOverrides(ServiceOverride.ForKey(typeof(ITestService)).Eq(MainServiceComponent))
.LifeStyle.Transient);
// This proxy should be created only if SiteInformation.PublishUrl is different from empty or null
container.Register(Component.For<IConfigurationPublicationProxy>()
.ImplementedBy<ConfigurationPublicationProxyWebService>()
.ServiceOverrides(ServiceOverride.ForKey(typeof(ITestService)).Eq(PublicationServiceComponent))
.LifeStyle.Transient);
有没有办法让温莎在解决之前评估一个条件?我知道它有条件注册,但我还没有找到一种方法来进行条件解析...提前谢谢!
【问题讨论】:
-
如果满足某个条件,您说它“不应该解决”。那是什么意思呢?您希望该服务的解析引发异常,还是执行其他操作?
-
我的意思是如果它可以被解析为null,那么我们可以稍后检查服务是否已经创建。
标签: c# asp.net-mvc asp.net-mvc-4 castle-windsor