Why asp.net session is expired sooner than its timeout?

前端 未结 3 1238
南旧
南旧 2021-01-06 21:55

I have an Asp.net webservice. It has method M1. M1 creates a folder for each session. When a session is expired, I delete that folder in global.asax using the following code

相关标签:
3条回答
  • 2021-01-06 22:34

    Have a look at this post, I believe your problem may be similar:

    • Client sends a cookie with a Session Id for an expired session
    • Server creates a new session, reusing the id sent by the client
    • If the request does not access session, the session is immediately abandoned.

    Does your WebMethod actually access Session state? If not, try adding an access to a dummy session variable.

    Update:

    These lines of code in Global.asax solve the problem:

     void Session_Start(object sender, EventArgs e)
     {
         Session["dummy"] = "dummy session for solving immediate session expire";
     }
    

    Update 2

    Personally I wouldn't do the directory creation in Session_Start; instead I'd have a method called something like EnsureMyPacksFolder which the app is required to call before any attempt to access the folder. This would look something like the following, avoids the need for a "dummy" Session variable, and means the folder is only created if and when it is actually needed.

    Global.asax:

    void Session_Start(object sender, EventArgs e)
    {
         // No code needed in Session_Start
    }
    
    void Session_End(object sender, EventArgs e)
    {
        if (Session["MyPacksFolder"] != null)
        {
            // Folder has been created, delete it
            // ... add code to delete folder as above
        }
    }
    

    Somewhere else:

    public static void EnsureMyPacksFolder()
    {
        if (Session["MyPacksFolder"] == null)
        {
            // Add code to create MyPacksFolder that was previously in Session_Start
    
            Session["MyPacksFolder"] = true;
        }
    }
    
    0 讨论(0)
  • 2021-01-06 22:40

    If you are creating and deleting folder under your web service path, iis immediatly restarts. Take a look on this post.

    0 讨论(0)
  • 2021-01-06 22:41

    Did you add the following to your WebMethods?

    [WebMethod (EnableSession = true)]
    
    0 讨论(0)
提交回复
热议问题