Question: I have a WCF service hosted in IIS 7. It takes some minutes until this service finish to load its data and ready for external calls. Data loads in internal thread. The problem is that from IIS point the service is ready just after it was activated (by some call), and it process a request without waiting for data to be loaded.

Is it possible to tell IIS that the service is still loading and make this service unavailable for requests? No problem if such request will throw an exception.

Answer: 
You could **invoke the initialization logic synchronously in the service's default constructor**. The service operations won't be invoked until the service instance has been created, which will only happen after the initialization has completed. In the meantime clients simply won't receive a response.

Here's a quick example:

```
public class MyService : IMyService
{
    public MyService()
    {
        // Blocking call that initializes
        // the service instance
        this.Initialize();
    }

    public void GetData()
    {
        // The service operation will be invoked
        // after the service instance has been created
        // at which point the initialization is complete
    }

    private void Initialize()
    {
        // Initialization logic
    }
}

```

If the initialization logic is expensive, you should consider making your service run as a singleton, so that **the price is paid only at the first request**. Alternatively you could store the data loaded during initialization in a centralized cache. This way it can be made available to multiple service instances while still having to load it only once.

If the initialization logic is shared across multiple services, you should consider implementing it once in a custom **[ServiceHost](http://msdn.microsoft.com/en-us/library/system.servicemodel.servicehost.aspx)** by overriding the **[OnOpening](http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.communicationobject.onopening(v=VS.90).aspx)** method. Since you're hosting your services in IIS, you would then also need to implement a custom **[ServiceHostFactory](http://msdn.microsoft.com/en-us/library/ms596415(v=VS.90).aspx)** to create instances of your ServiceHost.  
You can read more about this approach in [this MSDN article](http://msdn.microsoft.com/en-us/library/aa702697.aspx).
Found this new feature described in "ASP.NET 4 and Visual Studio 2010 Web Development Overview": <http://www.asp.net/LEARN/whitepapers/aspnet4/#0.2__Toc253429241>

The problem that it's requires IIS 7.5 running on Windows Server 2008 R2, but works with ASP.NET 2.0+