Link to home
Start Free TrialLog in
Avatar of countrymeister
countrymeister

asked on

Application_Start, Application_End & Session_End

I have a few things to confirm on these three events in the global.asax file

Application_Start - fires when the first user accessses the application.
Applicaton_end - when the last user's session's times out
Session_End - when the users's session times out.

Does closing the browser invoke the Session_End?
If the last user closes the browser does it invoke Application_End?

I have a web application where I need to clean up some temp files in a particular directory, I need to do that when the session ends or when the application ends.
The users access this website from 9 -5, so will the application end event always be invoked if there is no activity past the last session timeout.?

Will the application start event fire the next day when the users hit the website again? please note that I don't intend to start and stop IIS everyday.
Thanks
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

>>Applicaton_end - when the last user's session's times out => No: Application_End event is fired when the web site is stopped or IIS is reset

>>Does closing the browser invoke the Session_End? => No
>>If the last user closes the browser does it invoke Application_End? => No

>>I have a web application where I need to clean up some temp files in a particular directory, I need to do that when the session ends or when the application ends.
>>The users access this website from 9 -5, so will the application end event always be invoked if there is no activity past the last session timeout.?

I recommend you this using an ASPX page and call this page using Windows Task Schedular once a day at a specific time. Another option would be to place code in Session_Start event to delete files that are older than one day.


>>Will the application start event fire the next day when the users hit the website again? please note that I don't intend to start and stop IIS everyday => No

HTH, Nauman.
Avatar of countrymeister
countrymeister

ASKER

Thanks, so Session_End will only be invoked when the user's session time's out, meaning he is logged on for the timeout period with no activity.
Yes, Session_End is only invoked when the session is timed out or you call Session.Abandon from your code.

--Nauman.
Nauman,

Thanks for your help

This is what I came up with in my Session_Start and Session_End. In Session_Start i delete all files that are not equal to current date
In Session_End i delete user specific session file.
I tested this, and you are right that when I close the browser the session_end is not invoked as my file is not deleted.

       // Code that runs when a new session is started
        try
        {
            FileSystemObject fso = new FileSystemObject();
            string path = Server.MapPath(".");
            path = path + "\\PDFFiles";
            if (fso.FolderExists(path))
            {
                //delete each file in the folder, do not delete the folder as this would remove the folder privileges
                DirectoryInfo dirInfo = new DirectoryInfo(path);

                foreach (FileInfo f in dirInfo.GetFiles())
                {
                    if ((f.Extension.Equals(".pdf")) && ((f.CreationTime.ToShortDateString()) != (DateTime.Now.ToShortDateString())))
                    {
                       
                        f.Delete();
                    }

                }
            }


        }

        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.

        try
        {
            FileSystemObject fso = new FileSystemObject();
            string strFile = Session.SessionID.ToString() + ".pdf";
            string path = Server.MapPath(".");
            path = path + "\\PDFFiles";
            string pdf = path + "\\" + strFile;
            fso.GetFile(pdf);
            fso.DeleteFile(pdf, true);


        }
        catch (Exception ex)
        { }

ASKER CERTIFIED SOLUTION
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial