Disable the File Changes Monitor on subdirectories of the application root folder

Release 1.0 - ...

Most of the File Changes Monitors used by ASP.NET 2.0 are there for a good reason and it would not be recommended to disable them. Usually you want to unload the AppDomain(s) when the web.config or global.asax changes anyway. And that's just the thing these monitors will accomplish for you without having to worry about it.

However, there's one File Changes Monitor which is just annoying in most circumstances. And that is the monitor which detects if a subdirectory of the application root is being renamed or deleted. For instance, if you have an upload folder within the root directory and users are allowed to create and delete folders within it, each time a folder is deleted the AppDomain will be unloaded.

Thankfully, this particular File Changes Monitor can be effectively disabled using reflection. The example below shows a part of an adjusted global.asax, in which the Application_Start has been modified to disabe the above mentioned File Changes Monitor.

  CopyCode image Copy Code
<%@ import namespace="System.Diagnostics"%>
<%@ import namespace="System.Reflection"%>
<%@ Application Language="C#" %>
 
<script RunAt="server">
 void Application_Start(object sender, EventArgs e) {
 
   // stop FCN monitor on subdirs of application root directory
   PropertyInfo p = typeof(System.Web.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[] { }); 
 }
</script>

Note: The above mentioned File Changes Monitor will automatically be disabled in iXperion release RC2 and higher (unless the appSetting DisableSubfolderMonitoring in the smartsite.config file has been specified with the value 'false').