2012-10-04 41 views
0

我使用Castle DI繼承了一個項目。我將WCF添加到解決方案中,並需要使用解決方案中的一些功能。這個類有以下,我假設是注入。DI城堡WCF構造函數我如何

private readonly IOrderRepository _orderRepository; 
    private readonly IEshopOrderRepository _eShopOrderRepository; 
    private readonly IUserRepository _userRepository; 
    private readonly IListRepository _listRepository; 
    private readonly INHibernateRepositoryWithTypedId<ProductVariant, string> _productVariantRepository; 

    private readonly IMapper<GiftCardPayment, string, GiftCardPaymentDto> _giftCardDtoMapper; 
    private readonly IMapper<AbstractOrder, OrderDto> _orderDtoMapper; 
    private readonly IMapper<AbstractOrder, RecurringOrder> _recurringOrder; 
    private readonly IMapper<Address, CreditCardPaymentDto> _creditCardDtoMapper; 

    public delegate OrderDto ShipmentProcessing(OrderDto order, bool isRecap); 

    public OrderManagementService(IOrderRepository orderRepository, IUserRepository userRepository, IListRepository listRepository, 
     IEshopOrderRepository eShopOrderRepository, 
     INHibernateRepositoryWithTypedId<ProductVariant, string> productVariantRepository, 
     IMapper<GiftCardPayment, string, GiftCardPaymentDto> giftCardDtoMapper, 
            IMapper<AbstractOrder, OrderDto> orderDtoMapper, 
     IMapper<AbstractOrder, RecurringOrder> recurringOrder, 
     IMapper<Address, CreditCardPaymentDto> creditCardDtoMapper) 
    { 
     _orderRepository = orderRepository; 
     _eShopOrderRepository = eShopOrderRepository; 
     _userRepository = userRepository; 
     _listRepository = listRepository; 
     _productVariantRepository = productVariantRepository; 
     _giftCardDtoMapper = giftCardDtoMapper; 
     _orderDtoMapper = orderDtoMapper; 
     _recurringOrder = recurringOrder; 
     _creditCardDtoMapper = creditCardDtoMapper; 
    } 

所有這些工作。我的問題是如何在WCF服務類中實現這種模式。我無法將參數添加到構造函數中,因爲客戶端不會提供它們。

我能夠在WCF中使用DI,以便部分工作。

感謝

+0

這是一個很大你有沒有依賴性。您可能想要重構該類。看看[這裏](http://blog.ploeh.dk/2010/01/20/RebuttalConstructorOverinjectionAntipattern.aspx)。 – Steven

回答

1

城堡有一個WCF facility。實際上,它是一個自定義服務工廠,可以創建您的服務,而不是WCF提供的默認工廠。如果您想使用流利的註冊,則必須將服務提供給服務主機工廠中使用的容器。爲此,您可以創建自己的服務主機工廠,繼承Castle項目提供的服務主機工廠(Castle.Facilities.WcfIntegration.DefaultServiceHostFactory)並在工廠的構造函數中創建Windsor容器。

爲自己的服務主機工廠,從城堡提供的DefaultServiceHostFactory衍生的示例實現:

public class MyOwnServiceHostFactory: Castle.Facilities.WcfIntegration.DefaultServiceHostFactory 
{ 

    public MyOwnServiceHostFactory() : base(CreateKernel()) 
    { } 

    private static Castle.MicroKernel.IKernel CreateKernel() 
    { 
     var container = new Castle.Windsor.WindsorContainer(); 
     container.Install(new WindsorInstaller()); 
     return container.Kernel; 
    } 

} 


public class WindsorInstaller : IWindsorInstaller 
{ 

    #region IWindsorInstaller Members 

    public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store) 
    { 
     container.AddFacility<Castle.Facilities.WcfIntegration.WcfFacility>(); 
     container.AddFacility<Castle.Facilities.TypedFactory.TypedFactoryFacility>(); 

     container.Kernel.Resolver.AddSubResolver(new Castle.MicroKernel.Resolvers.SpecializedResolvers.ListResolver(container.Kernel)); 
     // add your services here... 
    } 

} 
+0

所以我使用NuGet來安裝城堡wcf設施。我必須從所有8個項目中刪除舊版本並安裝新版本。我現在有類似於Castle.Windsor.dll和Castle.MicroKernel.dll中存在類型'Castle.MicroKernel.Registration.Component'的錯誤。 - 哪些idaes會導致這些類型的錯誤? – user1069733

+0

現在不能檢查,但我假設你已經爲Castle.Windsor和Castle.MicroKernel使用語句,並且都包含一個類「組件」。如果您現在使用Component.For <>註冊服務,編譯器不知道要採取哪一個。您可以使用包括名稱空間(Castle.MicroKernel.Component)的類的全名或刪除對Castle.Windsor命名空間的引用。 –

+0

我爲服務主機工廠和容器配置添加了一個代碼示例... –