Microsoft Sucks - Exhibit 15,487

Tuesday, December 14, 2010

I have once again bumped into the bug feature wherein the ASP.NET framework will conveniently recycle the AppDomain if your code performs the UNTHINKABLE action of deleting a directory ( The first time I encountered this I spent Ed-knows-how-many hours trying to figure out why deleteing a directory was causing my session to drop - a completely unrelacted activity! After learning about this ~feature~ I ended up refactoring my code to avoid having to delete any directories. This time, however, I wasn't willing to back down so quickly.

Someone who is evidently a little bit smarter than me realized you could, using System.Reflection, simply turn off directory notification. I hereby present these six marvelous lines of code to anyone who has encountered this problem. I stuck them in the Application_Start() method of the Global.asax file and I can now delete as many directories as I like without having my session rudely stripped away from me.

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[] { });

< Previous: HTML Widgets <
> Next: Request.Url >

> Following: Another Microsoft Day >
Add a Comment


your name
your email (optional)