IIS messes with the application lifecycle in a way Tomcat wouldn’t dare

How would you dig it if someone randomly restarted your application every few minutes or so for absolutely no reason? Would you be upset, angry or plain confused on why an application server would bother recycling an application at seemingly random intervals without being prompted. See, just like the Servlet specification defines the ServletContextListener interface, it’s .NET counterpart happens to be Global.asax which defines application lifecycle methods like Application_End, Application_Start and such. A pretty sensible idea when you look at the surface of things but what’s horribly, horribly wrong is that .NET applications shut themselves down every 20 minutes if they detect that no requests have been made to IIS. This is all done in the hopes of preserving IIS resources which makes me wonder just exactly how resource intensive running an IIS app really is.

Now think about this, this means that any context loading or application initialization that you do on startup might be happening a few times an hour just because traffic to your application might be slow. Any static variables that you declare get dumped, any kron processes you’re running get killed, database connection pools get emptied, application scope data is erased, worker threads are stopped, everything is gone! If you’re running a plain old website which doesn’t do much, this might be borderline acceptable, but if you perchance happen to do some medium to heavy initialization, you’re bound to run into big time trouble that will leave you scratching your head and adjusting your crotch. And no, it doesn’t matter what you change in Web.config, nothing can save you.

If this isn’t appalling enough, try checking out some of the work-arounds to this problem. If you’re running a kron job in your application, you’ll realize that you’ll be doing some serious work just to keep it running. One of the arguments you hear is that since it’s a kron job, it really doesn’t belong in a .NET application and that you should really be writing a windows service to do the job for you. Now that’s just plain see-through bullshit, you can’t suggest writing a desktop application and call running a kron job in a web application a bad design The .NET gurus also suggest simulating a call to the web server to keep your application alive, something like the following:

void Application_End(Object sender, EventArgs e) {
   WebRequest request = WebRequest.Create ("http://myapp.com/SomePage.aspx");
   HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
   log.Info("Status: " + response.StatusDescription);
   response.Close ();
}

So pretty much, resuscitate it just before it’s about to croak. Truly a pathetic, pathetic piece of code which only exists because of a deep flaw in .NET”s application lifecycle philosophy. Compare this with the old and sturdy ServletContextListener which wouldn’t have the cahones to call contextDestroyed() even if the app was running for 10 years straight.

Another similar problem in .NET is the tendency for threads to store data in the HttpContext.Current object (kinda like ThreadLocal in Java), except that HttpContext is dependent on the Request object actually existing. If the Request is gone away (which only make sense since it’s an atomic event), any variables tied to it are also gone. I never comprehended why simple static space isn’t used instead.

Now someone might argue that you might be able to tweak settings in Machine.config or go to IIS Manager and drill down to Application Pools (new in IIS 6) and change your DefaultAppPool properties to alter the behavior of worker processes, which might do the trick. But the problem is that the default behavior of ALL IIS hosting companies is to leave this crucial setting at it’s default value and the worst part is that it can’t be altered at an application level but only in IIS. Something which just flat out sucks.

Advertisements

12 thoughts on “IIS messes with the application lifecycle in a way Tomcat wouldn’t dare

  1. Pingback: Web 2.0 Announcer

  2. Jon H

    Most Shared hosts will have time recycling set to around 24 hours, and automatically recycle the app pool if it hits a memory roof (usually ~100 megs).

    They also host each site in its specific appPool, so a misbehaving app on the shared server won’t cause well behaved apps to recycle.

    This is good from the hosting providers perspective, because it allows them to keep one run away application from tanking the server.

    If your application is causing constant recycles, take a look at the memory usage. You probably have a memory leak.

    Of course, since you can change this setting on each application Pool, and most hosting providers put each site in their own, you could always request them to tweak their settings if needed.

    Otherwise, get a VPS or a dedicated server. This is ASP.NET, we write real applications in this, you don’t stick those on shared servers anyways 🙂

    Reply
  3. alexanderthegreatest

    Every 20 minutes really isn’t unpredictable. You can change the interval if you’d like to.

    Reply
  4. Eric R

    You can also use the Microsoft.Web.Administration classes to programmatically change the server’s configuration. I come from a J2EE background (JBoss and Weblogic), and I now prefer IIS 7 over the J2EE stuff. It’s taken Microsoft 6 or 7 years to get where J2EE was then, but they’re definitely working to make the IIS platform flexible, extensible, and very developer-friendly. I think we’ll be seeing more people junping onto this platform once they figure out all the cool stuff available in IIS 7.x.

    Reply
  5. Iphone apps

    Good day! Would you mind if I share your blog with my twitter group?
    There’s a lot of folks that I think would really enjoy your content. Please let me know. Thanks

    Reply
  6. Britney

    Using a Debt Consolidation Loans network instead of going
    directly to a particular debt settlement company is to negotiate the reduction
    of new job opportunities. The first step is the Obama Credit Card debt consolidation
    loans Act by the President. Grants are also available
    whichprovide the customers with the necessary documentation that the debt consolidation loans grant.
    A savings account will make you feel any better,
    but the government offers grants forother things as well.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s