Welcome to EMC Consulting Blogs Sign in | Join | Help

Matt Hall's Blog

Experiences with Microsoft technologies

Castle Windsor and non-HTTP Protocol WCF Services

I had been working quite happily with Castle Windsor and WCF services using WCF Facility when implemented through an HTTP binding of some description. You simply hook in the DefaultServiceHostFactory from Castle.Facilities.WcfIntegration into your ServiceHost declaration such as the following example:

<%@ ServiceHost Language="C#" Debug="true" Service="MyService" CodeBehind="MyService.svc.cs"

Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, Castle.Facilities.WcfIntegration" %>

You would then register the WindsorContainer within the Application_Start event of your Global.asax like so or however you prefer register your components, the point being it is done in the Application_Start event.

protected void Application_Start(object sender, EventArgs e)

{

   var container = new WindsorContainer("ioc.config");

   DefaultServiceHostFactory.RegisterContainer(container.Kernel);

}

However, let’s say you are binding to net.tcp and hosting in WAS (Windows Process Activation Service)? This event will not get called, so you need to find another way to initialise your Castle Windsor container.

I confess I spent a while looking at the Host Factory in more detail, looking to wrap the DefaultServiceHostFactory. However, there appears to be a far simpler solution and that is to make use of the little documented AppInitialize method. If you create a class (any class), put it into the ASP.NET App_Code folder in your project and give it a method signature as defined below, this little baby will get fired exactly when you want it to. You can then initialise your IoC container in there.

public class InitialiseService

{

   /// <summary>

   /// Application initialisation method where we register our IOC container.

   /// </summary>

   public static void AppInitialize()

   {

      var container = new WindsorContainer("ioc.config");

      DefaultServiceHostFactory.RegisterContainer(container.Kernel);

   }

}
Published Tuesday, August 18, 2009 8:46 AM by Matthew.Hall
Filed under: , , ,

Comments

No Comments
New Comments to this post are disabled
Powered by Community Server (Personal Edition), by Telligent Systems