ASP.NET restarts when a folder is created, renamed or deleted
Asked Answered
F

5

17

UPDATE -- process to replicate issue:

1) Create a website project at c:\projects\restart-demo

2) Add default web.config and a dummy aspx page test.aspx

3) Map IIS to point to the root folder c:\projects\restart-demo

4) Monitor application restarts using perfmon, health monitoring, tracking in global.asax Application_End, etc.

5) Request page in browser http://localhost/test.aspx

application start

6) Create new folder c:\projects\restart-demo\asdf

application end

7) Request page in browser http://localhost/test.aspx

application start

8) Rename folder c:\projects\restart-demo\asdf to c:\projects\restart-demo\asdf1

application end

end update

We are using a back-end CMS to generate files and folders in an ASP.NET site.

Users are able to create/modify/delete files and push these out to the web farm.

One problem we have noticed:

When the user creates, renames or deletes a folder, it causes the App Domain to restart. As a consequence, session, cache, etc. are all lost.

Note it doesn't need to be a special folder like /bin or /App_Code either.

Is there any way to prevent this behavior?

It is really hampering performance for two reasons:

  • Cache is dumped when app domain restarts
  • App domain needs to be re-built after restart
Fug answered 12/2, 2010 at 0:28 Comment(5)
Does this happen 100% of the time, or when say, a certain number are created, like every 15th change?Deprecative
The behavior you describe is not typical. Can you give us some more contexual info- code maybe?Zelma
@Nick: This happens every time.Fug
@Nathan, I have posted steps to reproduce the issue.Fug
Assuming accepted answer fixed your issue. Can you tell me where you added below code and where did you called it from. I tried doing so in my Global.asax and controlller files without any luck. It will be great if you can edit your question and place that info at the end :)Stave
F
15

This code appears to resolve the issue, when added to Application_Start() in Global.asax:

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

http://dotnetslackers.com/Community/blogs/haissam/archive/2008/11/12/disable-session-expiration-when-using-directory-delete.aspx

With these changes, I can create/modify/delete folders without causing the application to restart.

Not clear if this is the best solution -- Don't know if there will be unwanted side effects due to calling StopMonitoring.

Fug answered 12/2, 2010 at 17:25 Comment(2)
Seems like a huge hack, but if it works I guess you don't have a choice. +1 for reporting back your solution.Fokos
Also there's a more complete example of how to do this here: aaronblake.co.uk/blog/2009/09/28/…Plebs
S
9

Perhaps a bit late, but storing and handling your temp files in a different folder outside the wwwroot of your application also solves the problem.

Steep answered 28/4, 2011 at 16:4 Comment(0)
D
2

By default an asp.net application will restart every 15th time a file changes within it's virtual directory, this is to outweigh partial recompilations and their memory weight vs overall performance...you can change this behavior, but memory use may rise and performance will drop off over time.

To do this, set the numRecompilesBeforeAppRestart attribute on the compilation element, your web.config would have en element like this:

<configuration>
  <system.web>
    <compilation numRecompilesBeforeAppRestart="15">

The default is 15, you can change it to whatever you want, read the link for more info. However, it's this way for a reason, it's not recommended to have your dynamic content inside the app's virtual directory, best to have it beside it or somewhere else entirely.

Deprecative answered 12/2, 2010 at 0:42 Comment(5)
Nothing is recompiling. I am just creating empty folders. Also, I have tweaked numRecompilesBeforeAppRestart and the Application_End still fires every time a folder is created/modified/deleted.Fug
@Fug - Can you update with an example of a few folders that are being created, in relation to the root?Deprecative
Added steps to replicate issue.Fug
@Fug - What OS are seeing this on? The FCN functionality differs, so it matters in this caseDeprecative
I have replicated the issue on Windows XP (IIS 5.1) and Windows 2008 (IIS7).Fug
A
2

Adding "fcnMode="Disabled" to the <httpRuntime> settings in the web.config disables AppDomain recycling when the contents of the web root folder are altered.

Afc answered 16/11, 2016 at 17:37 Comment(0)
T
0

Enable ASP.NET Health Monitoring and look in the event log to see why the AppDomain restarted.


I bet this is because you've used a web site project, instead of a web application project. Try to reproduce this with a web application project.

Also, do you have any anti-virus or indexing software running? Such software notices when folders are created and/or modified.

Thatcher answered 12/2, 2010 at 0:40 Comment(3)
Event message: Application is shutting down. Reason: Unknown.Fug
Thanks for checking. In this case, I suggest you look for anti-virus software and maybe indexing software. ASP.NET does not cause restarts whenever folders are created - something else is.Thatcher
Tried this on a separate machine without antivirus software and Indexing Service disabled. I still am finding Application_End is firing. I have tested on 3 separate machines.Fug

© 2022 - 2024 — McMap. All rights reserved.