tags:

views:

76

answers:

2

I have several applications I am deploying to a .NET/IIS environment. For these applications I have registered an implementation of IHttpModule in the system.web/httpModules section of web.config. In my implementation of IHttpModule I have an Init() method which starts up some daemon threads and logs that the application has been started.

My problem is that Init() does not get called until the first HTTP request comes in. Is there a way to force the system to go ahead and execute my module immediately instead of waiting for the first HTTP request?

A: 

You should move your code to the Global.asax file in the Application_Start method :

protected void Application_Start(object sender, EventArgs e)
{
    //your initialization code
}

Update :
As noted in comments, this is not a solution to the problem as Application_Start is also called on first request.

So it seems that the only solution is to find a way to call a web page as soon as IIS is restarted.

Here are some suggestions from another website :

One tricky solution to this problem can be adding your website to search engines. Search engines crawl pages frequently. So, they will hit a page of your website resulting in Application_Start and thus the service will get up and running.

Another idea is to register your website to some traffic or availability monitoring services. There are lots of web services which keep an eye on your website and checks whether it is alive and the performance is satisfactory. All these services hit your web site pages and then collect the statistics. So, by registering in such services, you can guarantee your web application is alive all the time.

I'd go with the monitoring service solution, with services such as (I didn't try them) :
http://www.montastic.com/
http://www.siteuptime.com/

Julien N
How would this be different? According to the Wikipedia page you linked, "At run time, upon the arrival of the first request, Global.asax is parsed and compiled into a dynamically generated .NET Framework class." This is consistent with the behavior I have seen with Global.asax.
Gene McCulley
You're right. I was quite sure that it would be fired when IIS launches the application.I updated my answer to add another solution (although it is more a workaround than a solution)
Julien N
+1  A: 

If you are on IIS7 you could use the IIS Application Warm up module to ensure your application is always running: http://learn.iis.net/page.aspx/688/using-the-iis-application-warm-up-module/

Check the application pool settings also, so it's not shutting down and restarting all the time whenever there is no activity.

Otherwise I'd recommend you create an NT Service and put your daemon threads there.

Hightechrider
I should have mentioned that this is IIS6.
Gene McCulley
Even if I could move the daemon threads into another process, I would still like the application to get through all of the startup process immediately rather than wait for the first HTTP request.
Gene McCulley
You could make an initial request as the last step in your deployment process and set the app pool to not recycle.
Hightechrider
Certainly I could just do the request trick and in fact for some of the sites that this code base serves a request does come in every five minutes from a service that checks availability. I could add that to all of the sites served. The reason I can't just do it at deploy is that the IIS server gets restarted sometimes as well and I would like the startup work done then and not paid by the first user after that.
Gene McCulley
Sounds like you will need a separate watchdog service that keeps an eye on IIS using either WMI to look for stopped application pools or some other simple 'dead-mans-handle' that can detect that your daemon isn't running. When you detect it's not running, issue a request, or use WMI to start it up again.
Hightechrider