Search code examples
wcfiiscastle-windsorcastlewas

HELP! - DefaultServiceHostFactory executing before application_startup and container creation


I am using the WCF facility for a service hosted in WAS (net.tcp binding in iis7) and experiencing a weird issue only upon a cold application startup (i.e. not already running).

The following statement should be executed upon first instantiation of my container.

DefaultServiceHostFactory.RegisterContainer(c.Kernel);

When the service is requested, I get the following exception in my WCF tracefile

Kernel was null, did you forgot to call DefaultServiceHostFactory.RegisterContainer()

The issue appears to be that the ServiceHostFactory is attempting to create an instance of the service's host before my container has been created.

Note:

  • This exception is happening BEFORE the Application_Start is executed
  • If the application is running (and the container has been initialised) then the service will operate as expected. The application can be started by going to the appropriate IIS site over HTTP or starting a debugging session from Visual Studio.

Steps to recreate issue

  • Issue a IISReset to shutdown all IIS app pools.
  • Call the service in question
  • WCF tracing spits out:

    System.ServiceModel.ServiceActivationException: The service '/abcd.svc' cannot be activated due to an exception during compilation. The exception message is: Exception has been thrown by the target of an invocation.. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException: Kernel was null, did you forgot to call DefaultServiceHostFactory.RegisterContainer() ? Parameter name: kernel at Castle.Facilities.WcfIntegration.WindsorServiceHostFactory`1..ctor(IKernel kernel) at Castle.Facilities.WcfIntegration.DefaultServiceHostFactory..ctor()


Solution

  • The problem is that global.asax and all its methods are related only to HTTP processing. Btw. class in global.asax is derived from HttpApplication which should make this pretty clear. Once you host application in WAS (which is case of net.tcp based binding) you can't use these methods. Try to use something like AppInitialize.