Like everyone, we never want our site to go down. Some pages display data and if something happens to the database we’d like at the very least to display old data instead of an error. In that vain I wrote a proof of concept that might help. This is not live and may never go live but it tests well and I thought it was interesting enough to post.
The implementation is based on two interconnected pieces – an OutputCacheProvider and an HttpModule. The OutputCacheProvider, like the built in output cache provider, is responsible for storing and retrieving cached pages. In addition, however, it keeps cached pages around past their expiration in case the need arises to render them. The HttpModule’s responsibility is to handle exceptions and by writing out cached versions of the page regardless of their age.
The sourcecode is available at on github but I’ll step through the initial check version of the code here to better explain what’s going on. ### GracefulDegradationOutputCacheProvider
This is just a standard OutputCacheProvider (OutputCacheProviders were introduced with ASP.NET 4.0. Check out http://weblogs.asp.net/gunnarpeipman/archive/2009/11/19/asp-net-4-0-writing-custom-output-cache-providers.aspx for more info). The interesting piece to note though is that the standard Get method implemented for OutputCacheProvider just calls another Get method that takes a boolean parameter called respectExpiration. The “standard” Get method just passes in true for this. Here’s the code: