2

これを機能させようとしていますが、何かが足りないはずです 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
);
4

1 に答える 1

5

あなたは行方不明TypedFactoryFacilityです。

container.AddFacility<TypedFactoryFacility>()
于 2012-04-18T01:18:20.227 に答える