global.asax 中这两个 OnlineUsers 方法有什么区别

发布于 2024-12-14 01:59:01 字数 3448 浏览 1 评论 0原文

请查看我的 global.asax :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using DataLayer;
using NiceFileExplorer.Classes;
using System.IO;
using System.Text;
using System.Data;
using System.Web.Hosting;

namespace NiceFileExplorer
{

    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            Application["OnlineUsers"] = 0;
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Application.Lock();

            OnlineUsers.InsertRow(
                Session.SessionID,
                DateTime.Now, //Session Start Time
                DBNull.Value, //Session End Time
                true);

            //OnlineUsers Is A Table In MS SQL SERVER 2008  

            //InsertRow  Is A StoredProcedure Of OnlineUsers Table Like Below :  


//    ALTER Procedure [dbo].[sp_OnlineUsers_Insert]
//    @Session_ID nvarchar(300),
//    @Session_Start datetime,
//    @Session_End datetime = NULL,
//    @Online bit
//As
//Begin
//    Insert Into OnlineUsers
//        ([Session_ID],[Session_Start],[Session_End],[Online])
//    Values
//        (@Session_ID,@Session_Start,@Session_End,@Online)

//    Declare @ReferenceID int
//    Select @ReferenceID = @@IDENTITY

//    Return @ReferenceID

//End

            Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
            Application.UnLock();
        }

        protected void Session_End(object sender, EventArgs e)
        {
            Application.Lock();

            OnlineUsers.UpdateRow_Some_Fields_By_SessionID(
                Session.SessionID,
                DateTime.Now,
                false);

//UpdateRow_Some_Fields_By_SessionID Is A StoredProcedure Of OnlineUsers Table Like Below :  

//    ALTER Procedure [dbo].[sp_OnlineUsers_Update_Some_Fields_By_SessionID]
//    @Session_ID nvarchar(300),
//    @Session_End datetime,
//    @Online bit
//As
//Begin
//    Update OnlineUsers
//    Set
//        [Session_End] = @Session_End,
//        [Online] = @Online
//    Where     
//        [Session_ID] = @Session_ID

//End

            Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
            Application.UnLock();
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

    }
}

我必须在我的项目中添加标签来显示 OnlineUsers!

1 正在使用:

Application["OnlineUsers"].ToString();

2 正在使用:

OnlineUsers.Count_Users().ToString();

    //Count_Users Is A StoredProcedure Of OnlineUsers Like Below :  
    //ALTER Procedure [dbo].[sp_OnlineUsers_Count]
    //    As
    //    Begin
    //        Select 
    //            [Session_ID],
    //            [Session_Start],
    //            [Session_End],
    //            [Online]
    //        From OnlineUsers
    //        Where 
    //            [Online] = 1

    //        Declare @Count int
    //        Select @Count = @@ROWCOUNT

    //        Return @Count
    //    End

有时 标签 1 向我们展示: 5
但标签 2 向我们展示:255

我对它们做错了什么?
为什么它们之间有很大的区别?

编辑
我在 web.config 中的 sessionState 是这样的:

<sessionState mode="InProc" cookieless="false" timeout="1" />

提前致谢

plz see my global.asax :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using DataLayer;
using NiceFileExplorer.Classes;
using System.IO;
using System.Text;
using System.Data;
using System.Web.Hosting;

namespace NiceFileExplorer
{

    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            Application["OnlineUsers"] = 0;
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Application.Lock();

            OnlineUsers.InsertRow(
                Session.SessionID,
                DateTime.Now, //Session Start Time
                DBNull.Value, //Session End Time
                true);

            //OnlineUsers Is A Table In MS SQL SERVER 2008  

            //InsertRow  Is A StoredProcedure Of OnlineUsers Table Like Below :  


//    ALTER Procedure [dbo].[sp_OnlineUsers_Insert]
//    @Session_ID nvarchar(300),
//    @Session_Start datetime,
//    @Session_End datetime = NULL,
//    @Online bit
//As
//Begin
//    Insert Into OnlineUsers
//        ([Session_ID],[Session_Start],[Session_End],[Online])
//    Values
//        (@Session_ID,@Session_Start,@Session_End,@Online)

//    Declare @ReferenceID int
//    Select @ReferenceID = @@IDENTITY

//    Return @ReferenceID

//End

            Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
            Application.UnLock();
        }

        protected void Session_End(object sender, EventArgs e)
        {
            Application.Lock();

            OnlineUsers.UpdateRow_Some_Fields_By_SessionID(
                Session.SessionID,
                DateTime.Now,
                false);

//UpdateRow_Some_Fields_By_SessionID Is A StoredProcedure Of OnlineUsers Table Like Below :  

//    ALTER Procedure [dbo].[sp_OnlineUsers_Update_Some_Fields_By_SessionID]
//    @Session_ID nvarchar(300),
//    @Session_End datetime,
//    @Online bit
//As
//Begin
//    Update OnlineUsers
//    Set
//        [Session_End] = @Session_End,
//        [Online] = @Online
//    Where     
//        [Session_ID] = @Session_ID

//End

            Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
            Application.UnLock();
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

    }
}

i have to labels in my project for showing OnlineUsers!

1 is using :

Application["OnlineUsers"].ToString();

2 is using :

OnlineUsers.Count_Users().ToString();

    //Count_Users Is A StoredProcedure Of OnlineUsers Like Below :  
    //ALTER Procedure [dbo].[sp_OnlineUsers_Count]
    //    As
    //    Begin
    //        Select 
    //            [Session_ID],
    //            [Session_Start],
    //            [Session_End],
    //            [Online]
    //        From OnlineUsers
    //        Where 
    //            [Online] = 1

    //        Declare @Count int
    //        Select @Count = @@ROWCOUNT

    //        Return @Count
    //    End

SomeTimes Lable 1 Shows Us : 5
but Lable 2 Shows Us : 255

what did i do wrong about them ?
why there is a big difference between them?

EDIT
my sessionState in web.config is like this :

<sessionState mode="InProc" cookieless="false" timeout="1" />

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回旧景 2024-12-21 01:59:01

Session_End 仅在会话超时时触发,因此这就是计数不匹配的原因。尝试使用 Session.IsNewSession 来关闭您的 Session_End 逻辑。

Session_End is only fired when the session times out, so that's why the counts don't match. Try using Session.IsNewSession to key off your Session_End logic instead.

捶死心动 2024-12-21 01:59:01

您的 OnlineUsers 表很可能包含大量从未被清理过的用户。

Session_End 不是一个非常可靠的事件,并且不会 100% 运行。例如,如果应用程序意外停止,则它不会为每个打开的会话运行 Session_End。然后,您将在 OnlineUsers 中看到一个永远不会被清理的用户列表。 Application["OnlineUsers"] 当然也不会被清理,但如果您的应用程序正在重新启动,那么它会被设置回 0,因此您不会注意到那里有很大的差异。

Most likely your OnlineUsers table is holding a large number of users that never got cleaned up.

Session_End isn't a very reliable event and doesn't run 100% of the time. For instance, if the application stops unexpectedly, it won't run Session_End for each open Session. You would then have a listing of users stuck in OnlineUsers that would never get cleaned up. Application["OnlineUsers"] of course also wouldn't get cleaned up, but if your application was restarting, then it's be set back to 0, so you're not going to notice that big difference there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文