title: Castle Windsor WCF integration - DefaultServiceHostFactory, Kernel, etc
body: <p>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.</p>
  
  <p>Previously (in a class called ServiceLocator.cs) there was code like this:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Register the WindsorServiceHostFactory with the container
      /// &lt;/summary&gt;
      public static void RegisterWcfServer()
      {
          RegisterWcfFacility();
          DefaultServiceHostFactory.RegisterContainer(Container.Kernel);
      }
  </code></pre>
  
  <p>where the RegisterWcfFacility() method looked like this:</p>
  
  <pre><code>    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&lt;WcfFacility&gt;();
      }
  </code></pre>
  
  <p>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):</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Register the WindsorServiceHostFactory with the container
      /// &lt;/summary&gt;
      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&lt;DefaultServiceHostFactory&gt;());
      }
  </code></pre>
  
  <p>And my new version of "RegisterWcfFacility()" looks like this:</p>
  
  <pre><code>    private static void RegisterWcfFacility()
      {
          var facilities = Container.Kernel.GetFacilities();
          var hasWcfFacility = facilities.Any(facility =&gt; facility.GetType() == typeof (WcfFacility));
  
          if (!hasWcfFacility)
              Container.AddFacility&lt;WcfFacility&gt;();
      }
  </code></pre>
  
  <p>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):</p>
  
  <p><a href="https://groups.google.com/forum/#!topic/castle-project-devel/VOQKW4XlvLM%5B1-25%5D" rel="nofollow">https://groups.google.com/forum/#!topic/castle-project-devel/VOQKW4XlvLM%5B1-25%5D</a></p>
  
  <p>thanks for any advice.  Cheers, -Dave</p>