tags:

views:

22

answers:

1

I have found a code snippet as given below which is used to disable file monitoring (file change notification) on IIS server, but the code is not working as expected. The monitor object below is getting NULL value. Not sure if there is some more additional code required or any other settings required. Can anyone suggest why this could be getting NULL value or suggest if there is any better way to doing this in C# -

//FIX disable AppDomain restart when deleting subdirectory

//This code will turn off monitoring from the root website directory.

//Monitoring of Bin, App_Themes and other folders will still be operational, so updated DLLs will still auto deploy.

System.Reflection.PropertyInfo p = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);

object o = p.GetValue(null, null);

System.Reflection.FieldInfo f = o.GetType().GetField("_dirMonSubdirs", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.IgnoreCase);

object monitor = f.GetValue(o); //Returns NULL

System.Reflection.MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); m.Invoke(monitor, new object[] { }); 
+3  A: 

I use the following code on .Net 3.5 SP1:

var theRuntime = typeof(HttpRuntime).GetField("_theRuntime", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null);
var fcmField = typeof(HttpRuntime).GetField("_fcm", BindingFlags.NonPublic | BindingFlags.Instance);

var fcm = fcmField.GetValue(theRuntime);
fcmField.FieldType.GetMethod("Stop", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(fcm, null);
SLaks
@SLaks - The code compiled fine but then we could not verify this on deployment build since there are some other issues which are holding us from doing so. However, since this is the only answer, I will be accepting this. Thanks a lot for the help.
Sachin Shanbhag