One of Castle Windsor’s facilities addresses wiring up of WCF services. So far, the sparse documentation for the WCF Facility seems to indicate that you have to configure your container in a global.asax. That’s not much to my liking. First of all, it reeks of ASP.NET, and secondly, it’s not going to work if you expose WCF over protocols other than HTTP.
However, now that we know that a custom ServiceHostFactory is effectively a Singleton, a much better alternative is to derive from the WCF Facility’s DefaultServiceHost class:
public class FooServiceHostFactory :
DefaultServiceHostFactory
{
public FooServiceHostFactory()
: base(FooServiceHostFactory.CreateKernel())
}
private static IKernel CreateKernel()
var container = new WindsorContainer();
container.AddFacility<WcfFacility>();
container.Register(Component
.For<FooService>()
.LifeStyle.Transient);
.For<IBar>()
.ImplementedBy<Bar>());
return container.Kernel;
Although it feels a little odd to create a container and then not really use it, but only its Kernel property, this works like a charm. It correctly wires up this FooService:
public class FooService : IFooService
private readonly IBar bar;
public FooService(IBar bar)
if (bar == null)
throw new ArgumentNullException("bar");
this.bar = bar;
#region IFooService Members
public string Foo()
return this.bar.Baz;
#endregion
However, instead of the static CreateKernel method that creates the IKernel instance, I suggest that the WCF Facility utilizes the Factory Method pattern. As the WCF Facility has not yet been released, perhaps there’s still time for that change.
In any case, the WCF Facility saves you from writing a lot of infrastructure code if you would like to wire your WCF services with Castle Windsor.
Remember Me
a@href@title, b, em, i, strike, strong
Page rendered at Saturday, February 04, 2012 10:08:16 PM (Romance Standard Time, UTC+01:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.