0

In asp.net 2, deleting a folder will cause the application to restart. But today I found that this behavior no longer existed in asp.net 4? I can't find any information about this. Can anybody tell me if this is true? If yes, that's awesome! I hate this application restart behavior in asp.net 2. You can try this:

Debug the following code in asp.net webforms (.net 3.5) and you'll catch the ThreadAbortException. But when you debug it in asp.net webforms (.net 4), you won't catch any exception; everything is fine. I think the reason why I got ThreadAbortException is that I deleted a directory in the thread. Then application restarted, and then the thread got aborted. (Environment: Win8, VS2012, IIS Express)

    protected void Page_Load(object sender, EventArgs e)
    {

        var thread = new Thread(() =>
        {
            var folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data\\Test");
            var filePath = Path.Combine(folderPath, "test.txt");

            if (Directory.Exists(folderPath))
            {
                Directory.Delete(folderPath, true);
            }
            Directory.CreateDirectory(folderPath);
            System.IO.File.WriteAllText(filePath, "Hello");

            try
            {
                var loop = 100;

                while (loop > 0)
                {
                    Thread.Sleep(100);
                    loop++;
                }
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
            }
        });
        thread.Start();

        Response.Write("Running");
    }
Mouhong Lin
  • 4,402
  • 4
  • 33
  • 48

1 Answers1

2

As fas i I can see there is not difference between 3.0 and 4.0, the following action will cause the app to restart:

Application Restarts

Modifying the source code of your Web application will cause ASP.NET to recompile source files into assemblies. When you modify the top-level items in your application, all other assemblies in the application that reference the top-level assemblies are recompiled as well.

In addition, modifying, adding, or deleting certain types of files within the application's known folders will cause the application to restart. The following actions will cause an application restart: •
Adding, modifying, or deleting assemblies from the application's Bin folder.

• Adding, modifying, or deleting localization resources from the App_GlobalResources or App_LocalResources folders.

• Adding, modifying, or deleting the application's Global.asax file.

• Adding, modifying, or deleting source code files in the App_Code directory.

• Adding, modifying, or deleting Profile configuration.

• Adding, modifying, or deleting Web service references in the App_WebReferences directory.

• Adding, modifying, or deleting the application's Web.config file.

MSDN 3.0 ASP.NET Application Life Cycle Overview

MSDN 4.0 ASP.NET Application Life Cycle Overview

So I guess it depends on which folder you delete in the appliction directory.

Aristos
  • 66,005
  • 16
  • 114
  • 150
Peter
  • 27,590
  • 8
  • 64
  • 84
  • Could you try my code (just added it to the post). I did get different result in asp.net 2 and asp.net 4. Thanks! – Mouhong Lin Feb 08 '13 at 17:38
  • @Mouhong Lin I do not think your app is restarting, however what are the directory paths (AppDomain.CurrentDomain.BaseDirectory) in 3.5 and 4.0? – Peter Feb 11 '13 at 09:01
  • I do fix this by disable the File Change Notification in app start (my project is using 3.5). So I think it's caused by restarting. AppDomain.CurrentDomain.BaseDirectory is same in 3.5 and 4.0, pointing to the website directory – Mouhong Lin Feb 20 '13 at 13:21