InProcセッション状態からセッション変数の複数のインスタンスを取得する際に問題が発生しました。
次のコードでは、単純なBusinessObjectをPage_Loadイベントのセッション変数に永続化します。ボタンをクリックすると、同じBusinessObjectの2つの新しく宣言されたインスタンスにオブジェクトを取得しようとします。
最初のインスタンスでプロパティの1つを変更するまで、すべてがうまく機能します。2番目のインスタンスも変更します。
これは正常な動作ですか?これらは新しいインスタンスであるため、静的な動作を示さないと思いましたか?
私が間違っているアイデアはありますか?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
' create a new instance of a business object and set a containg variable
Dim BO As New BusinessObject
BO.SomeVariable = "test"
' persist to inproc session
Session("BO") = BO
End If
End Sub
Protected Sub btnRetrieveSessionVariable_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRetrieveSessionVariable.Click
' retrieve the session variable to a new instance of BusinessObject
Dim BO1 As New BusinessObject
If Not Session("BO") Is Nothing Then BO1 = Session("BO")
' retrieve the session variable to a new instance of BusinessObject
Dim BO2 As New BusinessObject
If Not Session("BO") Is Nothing Then BO2 = Session("BO")
' change the property value on the first instance
BO1.SomeVariable = "test2"
' why has this changed on both instances?
Dim strBO1Property As String = BO1.SomeVariable
Dim strBO2Property As String = BO2.SomeVariable
End Sub
' simple BusinessObject class
Public Class BusinessObject
Private _SomeVariable As String
Public Property SomeVariable() As String
Get
Return _SomeVariable
End Get
Set(ByVal value As String)
_SomeVariable = value
End Set
End Property
End Class