user1069733
user1069733

Reputation: 485

DI Castle WCF constructor How Do I

I have inherited a project using Castle DI. I am adding a WCF to the solution and need to use some of the functions in the solution. The class has the following, which I am assuming is injection.

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;
    }

All of which works. My question is how do I implement this pattern in the WCF service class. I cannot add the parameters to the constructor because the client will not supply them.

I am able to use the DI in the WCF, so that part is working.

Thanks

Upvotes: 0

Views: 415

Answers (1)

Dirk Trilsbeek
Dirk Trilsbeek

Reputation: 6023

Castle has a WCF facility for that. In essence it is a custom service factory that creates your service instead of the default factory WCF provides. If you want to use the fluent registration, you have to supply your services to the container used in the service host factory. For that you can create your own service host factory inheriting from the one the castle project provides (Castle.Facilities.WcfIntegration.DefaultServiceHostFactory) and create the windsor container in the factories' constructor.

an example implementation for your own service host factory, derived from the DefaultServiceHostFactory supplied by castle:

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...
    }

}

Upvotes: 1

Related Questions