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.
Have a look at this post, I believe your problem may be similar:
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:
Update 2
Personally I wouldn’t do the directory creation in Session_Start; instead I’d have a method called something like
EnsureMyPacksFolderwhich 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:
Somewhere else: