Email Aaron Blake now at webmaster@aaronblake.co.uk
or contact me

Bug fix – Application restarts on directory delete in ASP.net

Introduction

If a directory within an ASP.net website is deleted while the site is running, this causes the whole site to reboot, loosing any session data and logging out users.

This quick tutorial will show you a quick fix that can be run in your global.asax to prevent this from happening.

Step 1 – Define the bug fix method

Define the following method somewhere in your application (easiest is within the global.asax file, but you can place it anywhere else that’s more convenient).

C#

private void FixAppDomainRestartWhenTouchingFiles()
{
if (CurrentTrustLevel == AspNetHostingPermissionLevel.Unrestricted)
{
// From: http://forums.asp.net/p/1310976/2581558.aspx
// 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.
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[] { });
}
}

VB.net

        Private Sub FixAppDomainRestartWhenTouchingFiles()
    If CurrentTrustLevel = AspNetHostingPermissionLevel.Unrestricted Then
        ' From: http://forums.asp.net/p/1310976/2581558.aspx
        ' 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.
        Dim p As PropertyInfo = GetType(HttpRuntime).GetProperty("FileChangesMonitor", BindingFlags.NonPublic Or BindingFlags.[Public] Or BindingFlags.[Static])
        Dim o As Object = p.GetValue(Nothing, Nothing)
        Dim f As FieldInfo = o.[GetType]().GetField("_dirMonSubdirs", BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.IgnoreCase)
        Dim monitor As Object = f.GetValue(o)
        Dim m As MethodInfo = monitor.[GetType]().GetMethod("StopMonitoring", BindingFlags.Instance Or BindingFlags.NonPublic)
        m.Invoke(monitor, New Object() {})
    End If
End Sub

Step 2 – Create Global.asax file in root of application.

If you don’t already have a global.asax file, you need to create one in the root of your application.

Step 3 – Run method in Application Start event of global.asax

C#

protected void Application_Start(Object sender, EventArgs e)
{
// Misc
FixAppDomainRestartWhenTouchingFiles();
}

VB.net

Protected Sub Application_Start(ByVal sender As [Object], ByVal e As EventArgs)     
' Misc     
FixAppDomainRestartWhenTouchingFiles() 
End Sub

Conclusion

This should fix the bug and prevent the ASP.net web site from restarting when a directory is deleted.

Further reading:
http://forums.asp.net/p/1310976/2581558.aspx

3 Trackbacks

  1. [...] the original post:  Bug fix – Application restarts on directory delete in ASP.net … By admin | category: asp.net script | tags: asp, deleted-while, directory-within, [...]

  2. [...] rest is here: Bug fix – Application restarts on directory delete in bASP/b.net b…/b SHARETHIS.addEntry({ title: "Bug fix – Application restarts on directory delete in bASP/b.net [...]

  3. [...] behaviour for ASP.Net to restart if a directory within the web site is deleted. I found this post which describes the problem and offers a solution whereby the following code is added to the [...]

Post a Comment

You must be logged in to post a comment.

Email me through this magic form.