为什么我的温莎隐式委托工厂注册不起作用

本文关键字:工厂 注册 不起作用 我的 为什么 | 更新日期: 2023-09-27 18:31:36

我正在尝试让它工作 - 但我一定错过了一些东西http://docs.castleproject.org/Windsor.Typed-Factory-Facility-delegate-based-factories.ashx#Registering_factories_implicitly_1

谁能发现它?

[TestClass]
public class UnitTest1 {
    [TestMethod]
    public void DelegateFactoryTest() {
        var container = new WindsorContainer();
        container.Register(
            Component.For<AComponent>().LifeStyle.Transient,
            Component.For<IAService>().ImplementedBy<AService>().LifeStyle.Transient
            );
        var comp = container.Resolve<AComponent>();
        Assert.IsNotNull(comp);
        Assert.IsNotNull(comp.GetService());
    }
    class AComponent {
        readonly Func<IAService> _serviceDelegate;
        public AComponent(Func<IAService> serviceDelegate) {
            _serviceDelegate = serviceDelegate;
        }
        public IAService GetService() {
            return _serviceDelegate();
        }
    }
    interface IAService { }
    class AService : IAService { }
}

收到以下错误

Castle.MicroKernel.Handlers.HandlerException:无法创建组件"Sandbox.Windsor.Tests.UnitTest1+AComponent",因为它具有需要满足的依赖项。"Sandbox.Windsor.Tests.UnitTest1+AComponent"正在等待以下依赖项:- 服务"System.Func'1[[Sandbox.Windsor.Tests.UnitTest1+IAService, Sandbox.Windsor.Tests, version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"未注册。

如果我明确注册,一切都很好

container.Register(
    Component.For<AComponent>().LifeStyle.Transient,
    Component.For<IAService>().ImplementedBy<AService>().LifeStyle.Transient,
    Component.For<Func<IAService>>().Instance(()=>new AService()).LifeStyle.Transient
);

为什么我的温莎隐式委托工厂注册不起作用

你错过了TypedFactoryFacility .

container.AddFacility<TypedFactoryFacility>()