Friday, October 10, 2014

Stayin' Alive

It's been a while since I posted.  I'm not sorry.  Life is busy.  You'll just have to learn to deal with missing me.  That's how our love works.

I recently had to figure out how to keep my .NET application "alive" or "awake" between requests.  This particular site belongs to a small business that is just getting started.  Since they're not sure how big their site might get we decided to stay small and use shared hosting from GoDaddy.  One of the problems they were encountering was that the site was always so "slow".  I couldn't figure out what they were talking about for quite some time until I finally hit the page early one morning, before anyone else had hit it for the day.  It was slow.  Obnoxiously slow.

IIS was recycling the app pools when no one visited the page for a period of time.  What I needed was a solution to make that stop happening.  I found a clever little solution that works for us, but has some obvious pitfalls.  H/t to Omar Al Zabir at Codeproject for this one.  If you actually clicked that link and read the original solution you may have noticed that it's quite old.  But who cares?  It works.

Basically, you create a cache item with a callback.  When the cache item expires and the callback fires, the callback hits your site.  I modified the solution a little bit to do what I needed.  I have a separate service from my website so I wanted to keep both of them alive.  Easy.  Check it out.

This is from the global.asax.cs from the web project:


That's pretty much the whole shebang right there.  Actually, that's not "pretty much" it.  That's it.  There are obviously some web.config values to add, but I think you can figure that out.