July 2, 2010

WCF facility for Castle Windsor - Service

In this blog post I will talk about using the WCF facility for Castle Windsor to create a WCF service in a hosted environment, like a ASP.NET Web Application or a WCF Service Application. In future posts I will discuss some extensions and other ways to configure, but first some basics.

This post is part of a blog post series about the WCF facility. See the introduction post for the other blog posts.

We all know that calling the container from your components is not good, but the WCF framework requires us to provide a parameter-less constructor. Luckily WCF has some extension points, so we can override the default behavior. One such extension point is that we can supply our own factory which will used to create the WCF service. The WCF facility supplies us with just such a factory, so we don't have to implement it ourselves.

To get things working we first have to add the WCF facility to our Windsor Container. If you don't have the dll's grab them from the demo project or compile your own from the source.

_container = new WindsorContainer().AddFacility<WcfFacility>();

Then we need to add the 'Factory' attribute to a .svc file. (I have removed all non essential stuff)

<%@ ServiceHost Service="WcfServer.DateService" Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, Castle.Facilities.WcfIntegration" %>

Now the correct factory will be used. It might be a mystery how that factory knows which container to use. But it's actually quite simple. When the WCF facility gets added to the Windsor container, it will take the IKernel that it is extending and will store it in internal static field. So this might be a problem if you have more than one Windsor Container with the WCF facility running in one web application.

Now we can add the interface with it's implementation like you would any other component. However you need to make sure that you name your component, the same name as you specified in the .svc file for the Service attribute.


Now for configuration you have two choices, either you can use WCF's app.config/web.config default configuration or you can use WCF facility to do the configuration. If you are going to use *.config approach, then you have to make sure that the name of the service has to be the same as the implementation. I'll talk about letting the WCF facility do the configuration a other time.

I've included a small demo project. This is just a service host. So you will need to use WcfTestClient or create your own project and create a reference to that project. Next blogpost I will talk about using the WCF facility to create WCF clients. So next post I will extend the solution to include a client.

Download project

1 comment:

  1. To learn .net, C#, Java, Silverlight, Visual Basic, Asp .net with advanced concepts, you can visit http://advanceddotnettutorial.blogspot.com