Injecting a WCF Channel as Dependency via Windsor

When working within a closed system which uses WCF and an IoC container you will often find the following pattern:

dependency%20of-.-%3E[AppCode],%20[SomeAppService%20Impl]delegates%20to-.-%3E[WCF%20Service%20Proxy],%20[SomeAppService%20impl]is%20injected%20into-[1]

 

What happens is the SomeAppService is often, but not always, a very thin wrapper over the WCF Service.  If you own both ends of this scenario its often nice to remove the WCF layer between.  Maybe you want a different deployment model in some situations such that the WCF service itself can be an in-proc reference.  Other times the front end is in a DMZ and the WCF services needs to be inside the corporate firewall.  Maybe its just easier to run on a dev machine.  Whatever the reason, you don’t always want to be tunneling through WCF.  Plus, its just cool to see it work.

To do this, you can use Windsor’s FactorySupportFacility to return a ClientBase<T>.Channel as the dependency instance. 

First, you’ll need a little class to expose the ClientBase<T>.Channel since it’s protected:

public class ServiceClient<T> : ClientBase<T> where T : class
{
    public new T Channel
    {
        get { return base.Channel;}
    }
}

Second, add the FactorySupportFacility to your WindsorContainer:

container.AddFacility<FactorySupportFacility>();

Lastly, register your implementation as either the WCF channel:

container
    .Register(Component.For<ISomeAppService>()
                       .UsingFactoryMethod(() => new ServiceClient<IWCFService>().Channel))

…or the actual implementation:

container
    .Register(Component.For<ISomeAppService>()
                  .ImplementedBy<WCFServiceImpl>())