1

My website creates a bunch of png files while converting documents from pdf. I have all those files uniquely identified and I want them to be removed at session end. My idea was to create a List, store it in Session and delete each file by path stored in my List<>.

In Global.asax I added:

void Session_End(object sender, EventArgs e) 
{
    if (Session["cleanUpCollection"] != null)
    { 
        List<String> deletePaths = ((List<string>)(Session["cleanUpCollection"]));
        foreach(String s in deletePaths)
        {
            try
            {
                System.IO.File.Delete(s);
            }
            catch { }
        }
    }
}

In web.config I added this:

<sessionState mode="InProc" cookieless="false" timeout="20" />

but files still sit in temporary location. I have yet to test where this fails but is there a better, common practice of deleting temporary files at session end? BTW, I verified and cleanUpCollection stores local server's paths so there is no error in file path.

ArtK
  • 1,157
  • 5
  • 17
  • 31
  • The way that you're going about this makes sense to me. Are you sure that the files aren't still in use? The Image and Bitmap classes have certain methods that REALLY like to hang onto references; if you don't dispose the class, the file reference is still open and the file delete will fail at session end time. – Lynn Crumbling Jun 06 '14 at 14:47
  • [This is the type of thing](http://stackoverflow.com/q/11408857/656243) I'm talking about. – Lynn Crumbling Jun 06 '14 at 14:49
  • [Same thing here](http://stackoverflow.com/q/3661799/656243). – Lynn Crumbling Jun 06 '14 at 14:50
  • @Lynn Crumbling - it looks like code in Session_End and Application_End does not get executed. I added some code to write log entries to txt file on Application_Start, end and Session_End.as well as Page_Unload for the page that generates those PNG files. In Unload event I asked the app to list all string items (file paths) that were just placed in the Session variable. Log entries are created properly on Application_Start and on Page_Unload. There are no entries from Session_End and Application_End events, thus leading me to a conclusion that events are not fired properly. – ArtK Jun 06 '14 at 20:47
  • My gut is that either the events aren't wired up, or you're getting an exception trying to write things/before you try to write. – Lynn Crumbling Jun 06 '14 at 21:04
  • I'd lean towards the first one. I was thinking along the lines of latter one so I had StreamWriter execute on the same file in different parts of the code where I could see the exception thrown. I also looked at IIS logs and found nothing. I know that file locking is not an issue because I can delete temp files on Page_Unload event (which does not do me any good because then browsers could not get to those files for page rendering). Any suggestions on checking Application_End and Session_End wiring? – ArtK Jun 06 '14 at 21:12
  • 1
    First - are you using any Request, Response, or Server objects in Session_End? If so, that's your exception. – Lynn Crumbling Jun 07 '14 at 00:57
  • @Lynn Crumbling - you may be on to something. I'm using Server.MapPath(). I'll check that tomorrow. I'll keep u posted. – ArtK Jun 07 '14 at 03:16
  • @Lynn Crumbling - hmm, I used your pointers, removed call to Server and stored path in Application variable and I have good and bad news. Good news, files get deleted, logs get created. Bad news - above only in development environment running on my IIS Express while debugging out of VS2012. When I publish to my test environment IIS7 on my test server, Session_End would not fire... – ArtK Jun 11 '14 at 14:16
  • Dollars to donuts that it's some sort of permissions issue. You're running as IUSR, so file system perms need to be set to allow that. – Lynn Crumbling Jun 11 '14 at 14:39

2 Answers2

3

A more robust approach to this would be to have a scheduled job running on your server (outside of your site) that periodically cleans up this folder, removing files older than a certain age. Relying on session timeout for this is not ideal.

I would also note that you have given us no indication of what error is occuring or why the files are not deleted. For more help on that, please provide some info.

Paddy
  • 33,309
  • 15
  • 79
  • 114
  • @Paddy - I kept the scheduled job in the back of my head. In the meantime I just added some code to write Session's activity to text file. So far I verified that items are correctly added and saved to Session and can be retrieved from the Session. Now I'm waiting for session to timeout. I ordered all exceptions to be written to text file. – ArtK Jun 06 '14 at 16:20
1

With the timeout set to 20 that means it will take 20 minutes before the session is abandoned and your code is executed. Also, Session_End event doesn't fire unless the session idles out and doesn't fire when someone navigates to another page or closes the browser so this code may not always clean up the directory.

Rather than waiting for the user session to end before deleting, I suggest deleting the files once the PDF has been created.

sharpguru
  • 340
  • 1
  • 7
  • 1
    -1 The Session_End event will always fire after the session times out. Saying that the code may not always clean up the directory if the user browses to another page or closes the browser is just blatantly wrong. – Lynn Crumbling Jun 06 '14 at 15:02
  • @sharpguru - just for clarification, I'm deleting .png files. I'm converting PDFs to PNGs so they can be viewed in any browser regardless of platform. – ArtK Jun 06 '14 at 15:12
  • @LynnCrumbling Really? because in the tests I just performed that isn't true. I close the app and the session_end never fires. Is there some setting that allows the asp application to continue to run and intercept events after the application lifecyle has ended? Asking honestly. I have files that need to be cleaned up regardless of the user action (specifically when they close the browser and walk away) - and the files remain in the Files directory long after session_end should have cleaned them up – Iofacture Aug 28 '18 at 23:59
  • @LynnCrumbling - I retract my statement. I see here you are talking about the actual session files or in memory data for the application and not some other externally copied files that are sitting on the file system (if I understand this correctly). I am looking for a way of cleaning up files I renamed to sessionID_filename.extension and copied to the Files directory on IIS - regardless of the users action and I have not found a reliable way of doing that. I am deleting them with session_end but this does not happen when the browser is closed – Iofacture Aug 29 '18 at 00:05
  • @LynnCrumbling After watching this for 24 hours - the only time files that are copied into the Files directory get deleted (if not caught by a session timeout or explicit session_end event i.e. not by a browser window close) is when the app pool gets recycled – Iofacture Aug 29 '18 at 14:42