简体   繁体   中英

create session after old session is timed out

i have a snippet that a session has numeric content nd it incrise to 3 but when it recieve to 3 it send to client.this session must time out in 1 minuet but until the session is not time out the program work correctly but when the session is expired and new session want create i get system.invalidoperationexception whats the problem???? its my code ?

 string ip = HttpContext.Current.Request.UserHostAddress;


           HttpContext failuser = HttpContext.Current;
           failuser.Session.Timeout =1;
           if (failuser != null)
           {
               if (failuser.Session[ip] != null)
                   failuser.Session[ip] = (int)failuser.Session[ip] + 1;
               else
                   failuser.Session[ip] = 1;

               // 
               retval = failuser.Session[ip].ToString();
               if ((int)failuser.Session[ip] > 2)
               {

                   retval = "!";

               }

You will not be able to maintain any session state after it expires.
A session timeout of 1 minute is very small. Keep it at something higher like 5 minutes.

It would be lot easier to not allow your session to timeout, rather than restore the session to the state where it was after a timeout.

Since you are already using WebMethods, you could extend the life of the session by posting back just before it expires. (Be sure to keep an absolute timeout, say after 30 mins if your user is inactive).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM