tags:

views:

223

answers:

2

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.

+1  A: 

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+

Kamarey
+1  A: 

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 by overriding the OnOpening method. Since you're hosting your services in IIS, you would then also need to implement a custom ServiceHostFactory to create instances of your ServiceHost.
You can read more about this approach in this MSDN article.

Enrico Campidoglio
I thought to use static constructor to do all initializations, but wanted to know if there is a better solution. Don't think that static constructor is right place for a such things, but will use it until the "IIS Application Warm-Up Module" will be released. So thanks for information.
Kamarey
Agreed, the static constructor of a class should only be used to initialize its static fields. As you said, the IIS Application Warm-Up Module is probably what you are really looking for. However I would still suggest you to move your service's initialization code to the OnOpening method of a custom ServiceHost. This would be the most appropriate way of doing service initialization, and corresponds to the Application_Start method of the Global.asax class in an ASP.NET web application.
Enrico Campidoglio