WCF Windsor Integration

time to read 2 min | 356 words

Last night I commited WCF Integration to the Castle trunk. This is based in part on the work described here by Orand.

The main idea here is that WCF should get the service instances from Windsor, which gives us all the usual advantages of IoC plus Windsor's interceptors capabilities. It isn't a lot of code, but it makes it much easier to work with WCF services.

Using it on a web application:

  • Call this in the beginning of the application (Global.Application_Start):
    WindsorServiceHostFactory.RegisterContainer(container);
  • Create a MyService.svc file and put this in it:
  • <%@ ServiceHost Service="component name in windsor" 
    Factory="Castle.Facilities.WcfIntegration.WindsorServiceHostFactory, Castle.Facilities.WcfIntegration" %> 
    
  • Have fun

Using it from a non-web application:

WindsorContainer windsorContainer = new WindsorContainer();

windsorContainer.AddComponent("operations", typeof (IOperations), typeof (Operations));
Uri uri = new Uri("net.tcp://localhost/WCF.Facility");
WindsorServiceHost host = new WindsorServiceHost(windsorContainer, typeof (Operations),
                                                 uri);
host.Description.Endpoints.Add(
	new ServiceEndpoint(ContractDescription.GetContract(typeof (IOperations)),
	                    new NetTcpBinding(),
	                    new EndpointAddress(uri)));
host.Open();

Obviously you can cut down on this code significantly using the standard WCF configuration.

Another interesting thing is the ability to apply cross cutting concerns using IServiceBehavior / IEndPointBehavior. Any components that are registered with those interfaces on the container will be applied to all the services that are created this way. You can apply individual behaviors using the standard WCF means.

What is this not?

It is not here to replace WCF configuarion, and it deals specifically just with creating/disposing of services using Windsor. There are already good tools to configure WCF, and this approach takes full advantage of them.

The code has both tests and a sample site, have fun.