
The root cause of Global Session_End not being executed(triggered)
If the website is to implement the online user function, the users must be added to the online list in the Session_Start method when they login, and they needs to be deleted from the online list in the Session_End method when they logout. Sometimes, the user is deleted in the Session_End method, but they cannot be deleted. It seems that the Session_End is not executed. We often think that there is a problem with the deletion code, but the user can be deleted normally without executing the delete user code in the Session_End method, indicating the code no problem, but there is a problem with the Session_End method. What is the root cause of the problem?
Let's first look at how the C# language stipulates the Session_End method:
1. If there is an error in the code in the Session_End method, it will return immediately, and it will not record the error or throw any exception, and the Respone.Write method cannot be used to output the error;
2. Methods or classes such as Server.MapPath, Request, Cache, HttpContext.Current cannot be used in the Session_End method;
3. It is necessary to set the sessionState mode="InProc" mode in the Web.Config configuration file to InProc.
So, if you encounter Session_End not being executed(triggered), you may wish to check whether the above regulations are violated first. The common mistake is to use Server.MapPath, Request, Cache, HttpContext.Curren in the Session_End method or the method it calls; if they are used, the code can be executed normally if they are implemented in another way.
Examples of Global Session_End not being executed(triggered)
1. Server.MapPath is used in the Session_End method
protected void Session_End(object sender, EventArgs e)
{
string path = "/temp/map.txt";
path = Server.MapPath(path);//Server.MapPath is not allowed in the Session_End method
}
2. The method called in the Session_End method uses HttpContext.Curren
protected void Session_End(object sender, EventArgs e)
{
string path = "/temp/map.txt";
path = Server.MapPath(path);//Server.MapPath is not allowed in the Session_End method
}
public void delOnline()
{
if(HttpContext.Current.Cache["user"] != null)
{
//It is not allowed to use HttpContext.Current and Cache in the method called in the Session_End method
string user = (HttpContext.Current.Cache["user"]).ToString();
}
}
-
Related Reading
- C# listview select row(a row and multiple rows)
- Solve the flickering problem of C# listview multithr
- C# convert punctuation marks to Unicode encoding
- C# Read and write to text file Newline, with one lin
- C# merge binary array(byte array)
- C# Winform button transparent background with proper
- Convert Unicode to string C#(with string to unicode)
- C# set Winform icon and version number
- C# float, double range, precision, definition and ty
- C# split string with \n pay attention to the problem
- C# Static constructor does not allow access modifier
- C# efficiency comparison of multiple strings concate