2009-12-21 27 views
0

我繼承了一些非常古老的ASP.Net代碼。最初寫入1.0,然後轉換爲2.0有一個頁面使用自定義尋呼機控制。該控件中有以下邏輯:會話變量移除問題。與頁面數據源相關

Private _DataSource As PagedDataSource 
Public Property DataSource() As PagedDataSource 
    Get 
     If Not IsNothing(Session("DataSource")) Then 
      _DataSource = Session("DataSource") 
      Return _DataSource 
     End If 
     Return Nothing 
    End Get 
    Set(ByVal value As PagedDataSource) 
     _DataSource = value 
     If Not IsNothing(value) Then 
      Session("DataSource") = value 
     Else 
      Session("DataSource") = Nothing 
     End If 
    End Set 
End Property 

它使用尋呼機有以下邏輯在裏面的頁面:

Private PagedData As PagedDataSource 

Private Function GetData() As DataTable 
    Dim DT As New DataTable 
    Dim CategoryID As Integer = -1 
    If IsNothing(ddlCategories.SelectedValue) OrElse Not Integer.TryParse (ddlCategories.SelectedValue, CategoryID) Then 
     CategoryID = -1 
    End If 

    Dim myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) 
    Dim myAdapter As New SqlDataAdapter 

    myAdapter = New SqlDataAdapter("SearchResources", myConnection) 
    myAdapter.SelectCommand.CommandType = CommandType.StoredProcedure 

    If SearchText <> String.Empty Then 
     trFiltered.Visible = True 
    End If 

    With myAdapter.SelectCommand.Parameters 
     .AddWithValue("@CategoryID", CategoryID) 
     .AddWithValue("@SearchText", SearchText) 
     .AddWithValue("@CompanyID", CompanyID) 
    End With 

    If Not Security.IsSiteAdmin Then 
     myAdapter.SelectCommand.Parameters.AddWithValue("@UserIsAdmin", 0) 
     myAdapter.SelectCommand.Parameters.AddWithValue("@FTPUserID", Security.GetUserID(Context.User.Identity.Name)) 
    Else 
     myAdapter.SelectCommand.Parameters.AddWithValue("@UserIsAdmin", 1) 
    End If 

    Try 
     myAdapter.Fill(DT) 
    Catch ex As Exception 
     ErrorLog.LogError(ex.Message, ex.StackTrace, HttpContext.Current.User.Identity.Name, HttpContext.Current.Request.Url.ToString) 
     HttpContext.Current.Response.Redirect("~/error.aspx", False) 
     Return Nothing 
    End Try 
    Return DT 
End Function 

Protected Sub ReloadData() 
    CurrentPage = 0 
    CheckFilters() 
    BindData() 
End Sub 

手頭的任務是消除所有的會話變量。如果不使用會話來表示這些數據,最好的方法是什麼。最初我被告知將所有會話項目放到一個cookie中,雖然這對大多數項目都有效,但由於cookie大小的限制,它無法工作,所以我也不會熱衷於將它保留在ViewState中,或者即使那是一個選擇。我很新的VB和沒有很多expierence重寫遺留代碼。會話不是一種選擇,因爲它正在被轉移到Web場中,並且粘滯會話被關閉,因此它必須在沒有會話的情況下工作。

任何建議,非常感謝。對不起,如果我問一個問題,有一個痛苦的明顯答案。

在此先感謝, -James。

回答

0

那麼,有人可能會來,並投票給我......但我會說你最好的選擇是ViewState。

如果您因爲不使用SessionState而受限制,並且運行時遇到Cookie大小限制,我只能考慮其他兩個選項(Cache和ViewState),我當然不會推薦Cache。

現在你知道你可以使用進程外會話狀態(SQL Server或StateServer)嗎?

更多信息可以在這裏找到: http://msdn.microsoft.com/en-us/library/ms178586.aspx

+0

確定,如果視圖狀態是最好的選擇,我只是有一個關於ViewState的問題。性能問題會每天發生30k次獨特點擊。這個代碼正在爲一個大流量的客戶運行,這個客戶的流量是不真實的?如果VieState沒有性能上的缺陷,那麼我可以沿着這條路走下去,但是隻是試圖從頂部開始,我確實得到了一個錯誤,告訴我我在viewstate中設置的是不可序列化的。我如何解決這個問題,並且非常重要的是不會出現任何性能問題。謝謝你的幫助。 – 2009-12-21 20:16:54

+0

ViewState的效果主要體現在客戶端,因爲它會增加頁面的整體頁面大小。 至於它的可序列化方面,你能給我一個特定的代碼行嗎? – 2009-12-21 20:35:34