Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to: Avoiding Cold-Start issues with IIS hosted WCF applications?

Tags:

iis

wcf

We have a system where we host a couple of WCF applications in IIS. Some of these applications have a bit of an extended start-up time taking a couple of seconds (more than a users would be happy to wait for). Once it's up and running, everything is snappy though, so it really is only the startup time.

The client also has a requirement to recycle the application pools every night.

Is there some way to wake all these services up so that the start-up time is not an issue for the first user of the system?

Initial thoughts where to write a windows service that would simply call a lightweight method on each service every x (configurable) minutes to keep the apps alive, but since we are in a load balanced environment, and the applications need specific host headers to be passed, we would always hit the load balance address which means there is no way for us to make sure that all services on all boxes in the cluster are actually started.

Since a single wcf application in IIS can only have a single host header, the only other way would be to setup a second iis web site pointing at the same application. I'm just not sure if that would do the trick since it would be in another host context.

another option could be to wright something like an extension to IIS (not sure if this is possible yet) that could call each of our services when IIS or the app pool actually starts up again. (Something that notices when the app pool has recycled but before the first user request.

Any ideas would be much appreciated.

Thanks

Gineer

like image 561
Gineer Avatar asked Feb 18 '10 16:02

Gineer


1 Answers

For those of us running on a version of IIS before 7.5, we are in the process of testing the following solution...

As mentioned in the original post, the initial idea was to fire off a WebRequest from a service running on each machine to the local web sites (which host the WCF services), but this would be impossible, since they all make use of Host headers, and they all live in a Network load balanced farm.

We then thought that we could simply provide the custom host headers in the web request to the localhost.

Turns out you can not update the host header name in a WebRequest. It’s a Read only field.

Messing with a proxy class makes it work though. See: http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/1b35c665-fe32-4433-8877-a62f2d400a8e/

And a short piece of my test code below in C#.

        WebRequest req = WebRequest.Create("<Correct Host name>");
        req.Proxy = new WebProxy("127.0.0.1");

        StreamReader stream = new StreamReader(
            req.GetResponse().GetResponseStream());

        StringBuilder sb = new StringBuilder();

        String LineString;

        while ((LineString = stream.ReadLine()) != null)
        {
            if (LineString.Length > 0)
                sb.Append(LineString);
        }
        stream.Close();

        String response = sb.ToString();

This may not be what the proxy class was intended for, but it seems to work either way.

Gineer

Ps. No, you do not need to have any actual proxy server installed on the local host machine.

like image 151
Gineer Avatar answered Oct 30 '22 14:10

Gineer