c# - 无法按约定注册 CaSTLe Windsor 组件

标签 c# castle-windsor

我似乎无法按照惯例注册我的 CaSTLe Windsor 对象,我真的不知所措。情况是我有两个项目,Website(一个真正基本的 Web 表单项目)和 BusinessObjects(一个类库)。我正在尝试使用 IoC 将我所有的业务对象实现保留在内部,并且只处理网站项目中的接口(interface)。

我发现实现此目的的最佳方法是使用安装程序。所以,在我的 Global.asax.cs 中我有这个:

private IWindsorContainer _container;

public override void Init()
{
    base.Init();

    InitializeIoC();
    Global.WindsorContainer = _container;
}

private void InitializeIoC()
{
    _container = new WindsorContainer();
    _container.Install(new BusinessObjectsInstaller());
}

public static IWindsorContainer WindsorContainer { get; private set; }

这似乎工作得很好,为了从容器中拉出一个对象,我只使用了一个非常简单的方法:

var thingey = Global.WindsorContainer.Resolve<IThingey>();

然后在我的 BusinessObjects 项目中,我有这个:

public class BusinessObjectsInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Component.For<IThingey>().ImplementedBy<Thingey>());
    }
}

现在,在这一点上,一切都按照我的预期进行。这不是优雅或任何东西,但我仍在努力解决这个问题。所以我现在要完成的最重要的事情是以更有用的方式连接这些对象,这让我用这个替换上面的组件注册:

container.Register(Classes
        .FromThisAssembly()
        .BasedOn<IThingey>()
        .LifestyleTransient()
        );

我绝对不能去上类。我得到的只是

No component for supporting the service BusinessObjects.Contracts.IThingey was found

最终,类注册将更改为其他人继承的另一个接口(interface),但一次一个步骤。

如果您能帮助我弄清楚发生了什么/我做错了什么,我们将不胜感激。

最佳答案

您没有指定由您的类注册的任何服务,因此默认情况下这些类被注册为它们自己的服务。来自文档:

By default the service of the component is the type itself

您必须指定组件注册的服务;您可以使用 WithService 属性或快捷函数(WithServiceBase()WithServiceDefaultInterfaces() 等)来执行此操作。链接资源包含您可以使用的不同选择方法:

Krzysztof Kozmic 建议您使用 Base 服务注册您的组件,您可以这样做:

container.Register(Classes
    .FromThisAssembly()
    .BasedOn<IThingey>()
    .WithServiceBase()
    .LifestyleTransient()
    );

关于c# - 无法按约定注册 CaSTLe Windsor 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22519081/

相关文章:

c# - 从 C# 确定父进程 ID

c# - 寻找通过 SSIS 从安全 Web 服务使用数据的示例

asp.net-mvc - Aspnet 样板/Aspnet 零慢 (IoC)

caSTLe-windsor - CaSTLe Windsor ArrayResolver 尝试实例化无法解析的数组依赖项

caSTLe-windsor - 温莎城堡 : A better way to implement 2 levels of (nested) factories?

c# - 这种选择使用哪个 C# 集合的策略缺少什么?

c# - 基于绑定(bind)数据的 MenuItem 模板更改

c# - 通过 .UsingFactoryMethod 解决但未使用内核明确解决的 transient 依赖项的生命周期是多少?

caSTLe-windsor - 使用 xml/app.config 配置 CaSTLe Windsor

c# - Selenium C# Webdriver 下拉菜单不会触发 Javascript