Home

Castle Stronghold

Windows Communication Foundation (WCF) Integration Facility

The WCF Facility allows for the use of the Windsor container for WCF Services.

Facility Summary
Uses ProxyNo
Requires configurationYes
Uses attributeNo
Version1.0 RC 3
Maturity levelLow

Introduction

Use the WCF Integration facility if you want the benefits of runtime dependency injection offered by the Windsor container and its facilities. Examples of utilizing such features are data access layers or other external dependencies of your service. Furthermore, the services registered with the container can use other facilities, such as the logging or transaction management. Finally, any extensions (i.e., IServiceBehavior) to the services that are registered with the container will be automatically applied to all services registered with the container.

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.

Quick start

  1. Instatiate the container in the Global.Application_Start, assuming you are hosting the service in IIS.
  2. Change the Service value in your .svc file to the component name in the windsor configuration (see below.)

Global.asax


        protected void Application_Start(object sender, EventArgs e)
        {
            WindsorContainer container = new WindsorContainer("windsor.xml");
            WindsorServiceHostFactory.RegisterContainer(container.Kernel);
        }
    

Example Web.config

Notice the service name here


        <system.serviceModel>
        <services>
            <!-- Before deployment, you should remove the returnFaults behavior configuration to avoid disclosing information in exception messages -->
            <service name="Castle.Facilities.WcfIntegration.Demo.UsingWindsor"
                     behaviorConfiguration="returnFaults">
                <endpoint
                                contract="Castle.Facilities.WcfIntegration.Demo.IAmUsingWindsor"
                                binding="basicHttpBinding"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="returnFaults" >
                    <serviceDebug includeExceptionDetailInFaults="true" />
                    <serviceMetadata httpGetEnabled="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
    
    
    

Example Windsor Configuration

Notice that the component.name==service.name from web.config. Also, the parameter will be passed to the service.


        <configuration>
            <components>
                <component id="my demo"
                           service="Castle.Facilities.WcfIntegration.Demo.IAmUsingWindsor, Castle.Facilities.WcfIntegration.Demo"
                           type="Castle.Facilities.WcfIntegration.Demo.UsingWindsor, Castle.Facilities.WcfIntegration.Demo">
                    <parameters>
                        <number>42</number>
                    </parameters>
                </component>
                <!-- Since this implements IServiceBehavior, it will be applied to all WCF Services registered with Windsor -->
                <component id="exceptionHandler"
                service="System.ServiceModel.Description.IServiceBehavior, System.ServiceModel"
                type="TimeWarner.Gis.Services.Common.Exceptions.LogExceptionHandler, TimeWarner.Gis.Services.Common">
        </component>
                
            </components>
        </configuration>

Example .svc file

Notice the Service value = the component.name from the Windsor configuration and the Factory value is our facility


    <% @ServiceHost Service="my demo" 
        Factory="Castle.Facilities.WcfIntegration.WindsorServiceHostFactory, Castle.Facilities.WcfIntegration" %>
        

Auto wiring

This facility will autowire any WCF extensions that are registered with the facility. In the above Quick Start the exceptionHandler component will be added to all services' behavior automatically. Also, if the service has a dependency that another facility can provide, such as ILogger, it will be autowired as well.

Quick Note

There is a sample in the source code for this facility.

Required Assemblies

Google
Search WWW Search castleproject.org