David Barrows
David Barrows

Reputation: 778

Castle Windsor WCF integration - DefaultServiceHostFactory, Kernel, etc

I'm nearly done with a big NHibernate upgrade that ended up also being a Castle upgrade. I'm nearly there except the ASP.NET website won't run, because I'm getting an error where ServiceSecurityContext.Current is null. I could be wrong (I'm still new to Castle) but I think it has something to do with the change I made to registering the WCF facility.

Previously (in a class called ServiceLocator.cs) there was code like this:

    /// <summary>
    /// Register the WindsorServiceHostFactory with the container
    /// </summary>
    public static void RegisterWcfServer()
    {
        RegisterWcfFacility();
        DefaultServiceHostFactory.RegisterContainer(Container.Kernel);
    }

where the RegisterWcfFacility() method looked like this:

    private static void RegisterWcfFacility()
    {
        IFacility[] facilities = Container.Kernel.GetFacilities();
        bool hasWcfFacility = false;
        foreach (IFacility facility in facilities)
        {
            if (facility.GetType() != typeof (WcfFacility)) 
                continue;

            hasWcfFacility = true;
            break;
        }

        if (!hasWcfFacility)
            Container.AddFacility<WcfFacility>();
    }

Subsequently I've changed it to this (because I was trying to get it to compile obviously, and the DefaultServiceHostFactory no longer has a "RegisterContainer" method):

    /// <summary>
    /// Register the WindsorServiceHostFactory with the container
    /// </summary>
    public static void RegisterWcfServer()
    {
        RegisterWcfFacility();

        // see:  http://stackoverflow.com/questions/9729395/castlewindsor-3-0-and-defaultservicehostfactory-registercontainer

        // obsolete:
        //DefaultServiceHostFactory.RegisterContainer(Container.Kernel);           

        Container.Register(Component.For<DefaultServiceHostFactory>());
    }

And my new version of "RegisterWcfFacility()" looks like this:

    private static void RegisterWcfFacility()
    {
        var facilities = Container.Kernel.GetFacilities();
        var hasWcfFacility = facilities.Any(facility => facility.GetType() == typeof (WcfFacility));

        if (!hasWcfFacility)
            Container.AddFacility<WcfFacility>();
    }

I'm just posting this mainly to ask things like: am I completely barking up the wrong tree? Is the way I'm registering this facility legitimate? Could any of this explain why my ServiceSecurityContext.Current is null? (and yes I have seen this):

https://groups.google.com/forum/#!topic/castle-project-devel/VOQKW4XlvLM%5B1-25%5D

thanks for any advice. Cheers, -Dave

Upvotes: 0

Views: 1741

Answers (1)

Paul Carroll
Paul Carroll

Reputation: 1887

I just had the same problem and found the answer here. Turns out you just an initialise class in the folder App_Code that looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Castle.Windsor;
using Castle.Facilities.WcfIntegration;

namespace YourNamespace
{
    public static class InitialiseService
    {
        public static void AppInitialize()
        {
            var container = new WindsorContainer();

            container.AddFacility<WcfFacility>();
        }
    }
}

Of course this relies on the WCF Castle Facility being installed from the package manager via:

install-package Castle.WcfIntegrationFacility

Hope this helps :)

Upvotes: 1

Related Questions