09 May 2009 - 4:21 AM / by Dominic Pettifer. 3 Comments for Stop IIS AppDomain Restarts When a Folder is Deleted.
Technical Article - I show you how to prevent IIS web server, and the ASP.NET worker process, from restarting your websites AppDomain whenever a folder is deleted (either programmatically or manually) in your web sites folder hierarchy.
If you've ever modified the web.config file for an ASP.NET application, or tinkered with the bin folder (added/deleted/renamed files etc.) while that website was running, you will have noticed that this causes the AppDomain to restart. All Session state is lost and Cached items removed as the website starts up again, any logged in users will be logged out (assuming you’re not using persistent cookies for authentication). Where I work, we often use the expression "Bouncing the web.config" meaning adding a space somewhere to the web.config file and saving it, forcing an AppDomain restart when we need it.
In ASP.NET 2.0, they added 'folder deletes' to the list of things that cause an AppDomain restart, and this causes serious problems with websites that support some form of file upload capability, along with the dynamic creation and deletion of folders to store those files. For instance, for a product in an ecommerce site, you might want to store the product’s images in a directory that derives its name from the product ID, eg. /productImages/123/ipod-nano.jpg, or even the ID for the image's record. This helps prevent filename conflicts with other uploaded files & images. Of course, when you come to delete the product from the database, you'd naturally want to delete its corresponding images and containing folders as well, but obviously can't because of the AppDomain restart issue. So we're left with leaving a whole bunch of empty folders lying around on our server (file deletes don't cause an App restart).
Fortunately, there is a solution thanks to the magic of Reflection and HttpModules. First create an HttpModule like so...
using System.Reflection;
using System.Web;
namespace MyWebsite
{
/// <summary>
/// Stops the ASP.NET AppDomain being restarted (which clears
/// Session state, Cache etc.) whenever a folder is deleted.
/// </summary>
public class StopAppDomainRestartOnFolderDeleteModule : IHttpModule
{
public void Init(HttpApplication context)
{
PropertyInfo p = typeof(HttpRuntime).GetProperty("FileChangesMonitor",
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
object o = p.GetValue(null, null);
FieldInfo f = o.GetType().GetField("_dirMonSubdirs",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
object monitor = f.GetValue(o);
MethodInfo m = monitor.GetType().GetMethod("StopMonitoring",
BindingFlags.Instance | BindingFlags.NonPublic);
m.Invoke(monitor, new object[] { });
}
public void Dispose() { }
}
}If you prefer to use the Global.asax file, place the Init() code in the Application_Start() event instead. I believe using the Global.asax approach has been deprecated in favour of using HttpModules which can respond to events fired by the web application lifecycle (on session start, session end, begin request etc.). The Init method acts the same way as Application_Start in Global.asax, and Dipose is similar to Application_End.
We want the above code to fire when the Application first starts up. Ther's no exposed way in the .NET framework to disable app domain restarts on folder deletes, so we need to use Reflection to dig deep into the HttpRuntime to achieve this. Now place this into the <httpModules> section inside the web.config file...
<add name="stopAppDomainRestartOnFolderDelete"
type="MyWebsite.StopAppDomainRestartOnFolderDeleteModule" />And that's it. This will prevent AppDomain restarts for folder deletes, but will still restart for web.config and bin folder modifications, exactly what we want.
This looks like the solution I'm looking for hours!!
But... I need to make exactly the opposite, I need to avoid AppDomain Restarts when the web.config is changed.
Is this possible?
In the other hand, if I don't want to use the Global.asax... Where should I place the code provided??
Please help me! I'll keep making attemps!
Posted on 2 November 2009 - 6:17 PM / by Ronny DJ
///This code stops AppDomain to Restart when I have modified my web.config file while my application is running
///I have used following code inside Application_End event of Global.asax
System.Reflection.PropertyInfo p = default(System.Reflection.PropertyInfo);
object o = null;
System.Reflection.MethodInfo m = default(System.Reflection.MethodInfo);
p = typeof(HttpRuntime).GetProperty("FileChangesMonitor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
o = p.GetValue(null, null);
m = o.GetType().GetMethod("StopMonitoring",System.Reflection.BindingFlags.Instance |System.Reflection.BindingFlags.NonPublic);
m.Invoke(o, new object[] { });
Posted on 12 November 2009 - 1:24 PM / by Chandra Bhushan
Excellent article! I've been struggling with how to download patches without the file watcher going crazy all the time. This combined with app_offline.htm should solve my problem.
Posted on 17 July 2009 - 10:18 PM / by Marcus McConnell
Lamborghini Countach - As a photo mosaic (from the blog Photo Mosaic Generator - Fun Adventures With Silverlight )
Red Bull gives you wings....that generate huge amounts of downforce #F1
about 18 hours ago from Twitterrific.vampire { -webkit-box-shadow: none; -webkit-box-reflection: none; } #cssjokes
7:44 PM July 30th from Echofon@edhenderson lol, lets get a trending topic going - .gangster .wrapper { color: #000; width: 150%; text-decoration: bling; } #cssjokes
7:36 PM July 30th from Echofon@weblivz I think the petition should be resubmitted but with security stuff taken out, as that's what the response purely focused on
6:13 PM July 30th from Echofon@weblivz I still think Chrome Frame can come to the rescue here, still keep their old browsers + legacy systems, no retraining costs etc.
6:12 PM July 30th from Echofon