InProc 세션 상태에서 세션 변수의 여러 인스턴스를 검색하는 데 문제가 있습니다.동일한 asp.net 세션 변수의 여러 인스턴스 검색 중 문제가 발생했습니다.
다음 코드에서는 간단한 BusinessObject를 Page_Load 이벤트의 세션 변수에 유지합니다. 단추 하나를 클릭하면 동일한 BusinessObject의 선언 된 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
나는 이것이 사실일지도 모른다고 생각했지만 증명하기가 어려웠다. 내 Business Objects는 꽤 깊고 깊이있는 사본이 필요하기 때문에 Brad Adams가 제안한 것처럼 IClonable을 피하고 클래스에 내 자신의 Copy 메서드를 만들었습니다 (http://blogs.msdn.com/brada/archive/2003/04). /09/49935.aspx – sw1sh