开发者

ASP.NET Session identifier

I'm trying to make a session code for each client on my site. In the Session_Start of the global asax I have this code:

private static long codSesion = 0;
private static object codSesionLocker = new object();
protected void Session_Start(object sender, EventArgs e)
{
    if (Session["CodSesion"] == null)
    {
        lock (codSesionLocker)
        {
            if (Session["CodSesion"] == null)
            {
                if (codSesion == 9999)
                    codSesion = 0;
                codSesion++;
                Session["CodSesion"] = codSesion;
            }
        }

    }
}

When I monitor this it seems to be perfect. After some time of inactivity starts the problems. One of the thing that happens is that too many times starts over from 1 (without reaching 9开发者_JAVA技巧999 and without IIS rebouting). And recently, even worst than taht: I'd used the codes 1 and 2. I stop using the server (leting the IIS without activity). After almost 1 hour (50 minutes) I tried again. The code the IIS gave me was 2 (in a different PC than the last code 2).

Please I'd realy apreciate any help with this.


I'm not sure if this explains all of the behavior you listed above, but by default, the application pool in IIS will recycle after a period of inactivity (these settings can be changed in IIS for each application pool). When the application pool recycles, then the new instance of the pool will be completely reinitialized.


After a certain period of inactivity, IIS will stop your application. Once somebody visits, the site will start again. Combine that with the fact that you're probably using In Processes Session State and that is why you are seeing the value reset.

You could try to use SqlServer to store the Session as described here so that the data is persisted:

MSDN - ASP.NET Session State

Even if that works, trying to use Session in this fashion is a bad idea. Depending on what you're trying to achieve, there is a better way to handle things.


IIS recycles the worker process and there you'll lose the static value of codSession.

Save the information in a file or database to overcome this issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜