Solved

Application_Start, Application_End & Session_End

Posted on 2006-11-20
5
1,457 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:countrymeister
  • 3
  • 2
5 Comments
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17979878
>>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.
0
 
LVL 1

Author Comment

by:countrymeister
ID: 17979987
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.
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17980336
Yes, Session_End is only invoked when the session is timed out or you call Session.Abandon from your code.

--Nauman.
0
 
LVL 1

Author Comment

by:countrymeister
ID: 17980725
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)
        { }

0
 
LVL 25

Accepted Solution

by:
nauman_ahmed earned 250 total points
ID: 17980844
I think the top portion of the code will be able to handle the deletion. There will be no need for the code in session_End event.

-Nauman.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question