为什么asp.net会话比其更早超时过期? [英] Why asp.net session is expired sooner than its timeout?

查看:102
本文介绍了为什么asp.net会话比其更早超时过期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Asp.net web服务。它有方法M1。 M1为每个会话一个文件夹。当会话过期,我用下面的code删除的Global.asax文件夹。

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);
        }
    }

当我在浏览器中打开我的web服务,并调用M1,其正常运行和文件夹上,我已设置超时到期时间删除。但是,当我提出调用Web服务的按钮,第二次,它的会话(会话超时和文件夹被删除后)启动(创建文件夹)和结束(删除文件夹)马上,我已设置超时之前。

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.

为什么出现这种情况?

如果我打开一个新的窗口(新会)为每个方法调用一切正常。但我有问题,当我点击调用按钮进行第二次。这多少有点像为同一会话缓存的问题。

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:


  • 客户端发送一个会话ID的cookie用于会话过期

  • 服务器创建一个新会话,把通过客户端发送的ID

  • 如果该请求不访问会话,会话将被立即抛弃。

请问您的WebMethod实际访问会话状态?如果没有,尝试添加到虚拟会话变量的访问。

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

更新:

在code的这些行的 Global.asax中解决问题:

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";
 }

更新2

我个人不会做在在session_start的目录创作;而不是我有一个名为像 EnsureMyPacksFolder 该应用程序所需的任何尝试访问该文件夹之前调用的方法。这看起来像以下,避免了一个虚拟Session变量的必要性,并表示如果当实际需要的文件夹只创建。

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中:

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
    }
}

别的地方:

public static void EnsureMyPacksFolder()
{
    if (Session["MyPacksFolder"] == null)
    {
        // Add code to create MyPacksFolder that was previously in Session_Start

        Session["MyPacksFolder"] = true;
    }
}

这篇关于为什么asp.net会话比其更早超时过期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆