2012-04-17 60 views
2

我想讓這個工作 - 但我必須缺少的東西 http://docs.castleproject.org/Windsor.Typed-Factory-Facility-delegate-based-factories.ashx#Registering_factories_implicitly_1爲什麼我的Windsor隱式代表工廠註冊不起作用?

任何人都可以發現它嗎?

[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'正在等待以下依賴項: - Service'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 
); 
+0

應該有關於需要哪些確切依賴關係的附加信息。也許還有更多的錯誤? – 2012-04-17 18:55:39

+0

是的,它說它需要我試圖免費獲得 - 添加到錯誤 – 2012-04-17 18:59:13

+0

嘗試交換component.for定義。先註冊IAservice,然後註冊AComponent。 – 2012-04-17 19:05:55

回答

5

你錯過TypedFactoryFacility

container.AddFacility<TypedFactoryFacility>() 
+0

謝謝,您是對的,而且示例中的嵌套類必須是公共的 – 2012-04-18 06:59:41