2

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.

void Session_End(object sender, EventArgs e)
    {
        try
        {
            System.IO.DirectoryInfo dirMyPacksFolder = new System.IO.DirectoryInfo(Utilities.getMyPacksFolder(Session));
            //dirMyPacksFolder.Parent.CreateSubdirectory("ended_" + Session.SessionID);
            if (dirMyPacksFolder.Exists)
            {
                dirMyPacksFolder.Delete(true);
            }
        }
        catch (Exception ex)
        {
            Utilities.logException("", ex);
        }
    }

When I open my webservice in browser and call M1, it operates correctly and the folder is deleted on timeout expiration time that I have set. But when I submit "Invoke" button of webservice for the second time (after session timeout and folder is deleted), its session starts (create folder) and ends (deletes folder) immediately before timeout that I have set.

Why this happens?

If I open a new window (new session) for each method call everything is OK. But I have problem when I click "Invoke" button for second time. It is something like caching problem for same sessions.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Bob
  • 22,810
  • 38
  • 143
  • 225

3 Answers3

3

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;
    }
}
Joe
  • 122,218
  • 32
  • 205
  • 338
  • don't you use `Session_Start` and `Session_End` for ***Count*** users ? I use WebFarm, sessionsate mode SqlServer. Any source code samplefull? Link not found, use http://thecodeking.co.uk/article/asp-net-session-end-event-fires-immediately-after%20session-start-resolved/ – PreguntonCojoneroCabrón Nov 05 '16 at 13:17
0

Did you add the following to your WebMethods?

[WebMethod (EnableSession = true)]
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
0

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

Community
  • 1
  • 1
hkutluay
  • 6,794
  • 2
  • 33
  • 53
  • I know this. No I am creating folders outside of application's main folder. And I have tested and know that Application_End is not triggered. – Bob Jul 07 '12 at 11:55
  • You also need enable session on client code.. Just try to write a client application (not test on browser) and assign cookie container to it. http://www.drbob42.com/examines/examin47.htm – hkutluay Jul 07 '12 at 11:59