views:

84

answers:

2

Hello everyone,

I am using ASP.Net + .Net 3.5 + VSTS 2008 + IIS 7.0 + C# to develop a web application. I am creating a very simple web application, and I just modified page_load, Session_Start and Session_End. Here are my code, my question is I find session ends message is written to file immediately after session start message. My test method is just open IE to access this page. I think session should last for a long time, like several mins. Why session ends so quickly? Why seesion ends even if I do not close the page?

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Hello World! ");
    Session["SomeUserData"] = DateTime.Now;
}

protected void Session_Start(object sender, EventArgs e)
{
    using (TextWriter tw = new StreamWriter(@"C:\TestSession\bin\session.txt", true))
    {
        // write a line of text to the file
        tw.WriteLine(DateTime.Now + " Session Started.");
    }
}

protected void Session_End(object sender, EventArgs e)
{
    using (TextWriter tw = new StreamWriter(@"C:\TestSession\bin\session.txt", true))
    {
        // write a line of text to the file
        tw.WriteLine(DateTime.Now + " Session Ended.");
    }
}

I met with the same issue even if add Session manipulation code into Page_Load method, here is output file content, which you can see session ends event is called immediately after session start.

2010/7/15 0:11:14 Session Started. 2010/7/15 0:11:14 Session Ended. 2010/7/15 0:11:28 Session Started. 2010/7/15 0:11:28 Session Ended.

thanks in advance, George

+1  A: 

This kind of behaviour is sometimes caused by an overzealous anti-virus program. Try disabling the anti-virus and see if that helps. If it helped, then add the site's directory to the exclusion list.

Also closing the browser doesn't have any effect on your session either way.

Johann Strydom
You mean closing browser does not mean end session immediately (means Session_End is not called immediately from server side)? Why?
George2
Session end is only triggered when the session times out by itself or when it is forcefully ended by calling Session.Abandon().Typically the server has no idea that the browser was closed (unless you have some complicated javascript to make a call to the server when the browser closes and that doesn't even work in all scenarios). That is why the session has a timeout so it doesn't live forever.
Johann Strydom
I understand your reply, but it does not reply what I am asking. :-)I am asking why Session End is called immediately after Session Start in my sample?
George2
To your question "why Session End is called immediately after Session Start in my sample" my answer is that it might be the Antivirus.
Johann Strydom
How do you prove it?
George2
+2  A: 

If I remember correctly, you need to actually assign something to Session to activate it. Otherwise ASP.NET will regenerate a new session (new session id if you use fiddler to check) every time.

EDIT: Because session.txt is located inside the bin directory. Once you write to the bin directory the application will restart so the Session_End event fired immediately. Move session.txt out of bin directory to somewhere else will resolve your issue.

airmanx86
"you need to actually assign something to Session to activate it." -- could you show me how?
George2
I mean setting a session variable e.g. Session["UserData"] = "userdata";. ASP.NET does not actually keep the session if the code never write anything into session.
airmanx86
Try to add one more line like this Session["UserData"] = "userdata"; into Page_Load event and see if it works for you now.
airmanx86
I have tested your solution does not work. Please refer to my editted original post for my new code to manipulate Session in Page_Load, and also my output log which shows Session Ends invoked immediately after Session Start.
George2
@George2, I work out why, it is because session.txt is located inside the bin directory. Once you write to the bin directory the application will restart so the Session_End event fired immediately. Move session.txt out of bin directory to somewhere else will resolve your issue.
airmanx86
"Once you write to the bin directory the application will restart so the Session_End event fired immediately." -- why?
George2
I tried your conclusion is correct, I want to know why moving to other directory works?
George2
The behaviour is by design, the bin directory is special in ASP.NET and it is suppose to store the assemblies needed for the application. The reason is to do with how ASP.NET monitor the bin directory for any changes made to assemblies inside it and then restart the application to load all the assemblies again.
airmanx86
Cool, question answered!
George2