Solved

Application_Start, Application_End & Session_End

Posted on 2006-11-20
5
1,465 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
daypilot scheduler 3 35
Client Validating 2 date fields, required & comparison 1 42
asp.net mvc5 6 20
Need syntax help building hyperlink 2 22
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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