1

Possible Duplicate:
ASP.NET Session & Delete Folders

There are many questions about similar behaviour here:

Session State is erased after calling RedirectToAction() in ASP.NET MVC 3
MVC3 destroying session on redirecttoaction
Session variable is lost on RedirectToAction in IE

However the scenario I'm encountering is where I have a controller action that deletes some files. My application tracks various uploaded files for users. When a user is deleted I clean up the assets folder for that user. The assets for a user can contain several folders containing different types of uploads (images, documents, videos and so on).

The code looks something like:

if(DirectoryExists(userRootFolder))
{
  try
  {
    // Delete all of user's files and folders
    Directory.Delete(userRootFolder, true);
  }
  catch(Exception ex)
  {
    // Not a show stopper but we need to know
    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
  }

  return RedirectToAction("Index");
}

If a System.IO.IOException exception is thrown due to:

System.IO.IOException: The directory is not empty.

...then in the action called immediately after RedirectToAction my Session has been destroyed.

As an experiment I replaced Directory.Delete(userRootFolder, true); with:

throw new Exception("Oops!");

and also (the exception being thrown by Directory.Delete()):

throw new System.IO.IOException("The directory is not empty.");

But that doesn't cause my session to be destroyed.

I also tried deleting a file that isn't there to force a System.IO.DirectoryNotFoundException exception to be thrown and that's fine as well, my session stays intact.

I also used Fiddler to check to see if my Session cookie was being deleted or modified but that remains intact.

This problem happens with both Debug and Release builds and whilst running under the VS2010 (SP1) debugger on IIS Express and Cassini and on a full fat Windows 2008 R2 IIS7.5 box.

Is there something so catastrophic about trying to delete a folder that's not empty that would tear down my in-proc session?

Community
  • 1
  • 1
Kev
  • 118,037
  • 53
  • 300
  • 385
  • may be because IO exception signifies that the application is low on resources so to free the resources the session is cleared out ... – John x Mar 19 '12 at 19:06
  • >>IO exception signifies that the application is low on resources so to free the resources the session is cleared out -- I don't think so but see if you can repro Can you come up with a super simple repro? Why don't you throw System.IO.IOException? Also, have a catch for System.IO.IOException too. – RickAndMSFT Mar 19 '12 at 19:35
  • @Rick.Anderson-at-Microsoft.com - heh after 10 years as an ASP.NET developer I can't believe I fell for the old "delete any folder and your web app restarts" gotcha. – Kev Mar 20 '12 at 00:20

0 Answers0