Proper place for initialization code in non-HTTP WCF service hosted in IIS/WAS?

853 views Asked by At

It is my understanding that a WCF service configured for net.msmq will not run as an HttpApplication when hosted in IIS/WAS, which means you don't get the events in Global (Application_Start being the important one). Am I correct in this regard?

Does using Windows Server AppFabric to auto-start the service hit the service over HTTP, causing the Application_Start event to fire? If so, is this reliable?

I guess my real question is, where is the "proper" place for initialization code for a non-HTTP WCF service?

Particularly (since the right place may be different for different stuff):

  • IoC registrations
  • log4net initialization
  • Verifying the MSMQ queue exists locally (as Juval Lowy suggests in his WCF book)

Follow-on question: Do the events in a custom IServiceBehavior fire on every request or only once at startup? What about ServiceHost.OnOpening() - every request or only once at startup? (My testing shows it's only once, but does anyone know where the WCF "lifecycle" is documented, because I can't seem to find it anywhere.)

1

There are 1 answers

0
Sixto Saez On

I can't verify that WCF services hosted in IIS/WAS can't be configured to support Application_Start but it is not required to do what you want. I'd recommend you create a custom ServiceHost in conjunction with a custom ServiceHostFactory (for WAS). This approach is documented in an MSDN article and this blog post. Using this approach, you can host your service in IIS/WAS, a Windows Service or a console app because the initialization process will be the same.