2011-11-07 23 views
0

PLZ看到我的Global.asax:是什麼betweeen這兩個OnlineUsers方法的區別在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" /> 

在此先感謝

回答

2

Session_End僅在會話超時時觸發,所以這就是計數不匹配的原因。嘗試使用Session.IsNewSession來取消您的Session_End邏輯。

+0

真的很感謝關注! PLZ看到我的編輯,之後你會plz給我一些關於Session.IsNewSession的解釋! – MoonLight

2

最有可能你的OnlineUsers表持有大量的用戶是從來沒有得到清理。

Session_End不是一個非常可靠的事件,並且不會100%運行。例如,如果應用程序意外停止,它將不會爲每個打開的Session運行Session_End。然後,您將擁有一個永久不會清理的OnlineUser中的用戶列表。應用程序[「OnlineUsers」]當然也不會被清除,但是如果您的應用程序正在重新啓動,那麼它將被設置回0,所以您不會注意到那裏的巨大差異。

+0

是。 Session_End非常脆弱,幾乎沒用,恕我直言。 –

+0

感謝您的回答,那麼我們可以對OnlineUsers在.net中做些什麼?展示他們的最佳方式是什麼? – MoonLight

+0

@MoonLight老實說,我不知道。這不是我曾建議某人親自添加到網站的功能。我會在這裏搜索,看看是否有什麼適合你:http://stackoverflow.com/search?q=%5Basp.net%5D+show+online+users –