检索同一 asp.net 会话变量的多个实例时出现问题
我在从 InProc 会话状态检索会话变量的多个实例时遇到问题。
在以下代码中,我将一个简单的 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
I'm having problems with retrieving multiple instances of a session variable from an InProc session state.
In the following code I persist a simple BusinessObject into a session variable on the Page_Load event. On the click of a button I try to retrieve the object back into 2 new declared instances of the same BusinessObject.
All works great until I change one of the properties in the first instance, it changes the second instance as well.
Is this normal behaviour? I would have thought as these were new instances they wouldn’t demonstrate static behaviour?
Any ideas where I'm going wrong?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的BO1和BO2是同一个对象
BO1是引用内存中某个区域的名称;
BO2 是引用同一内存区域的另一个名称; Session("BO") 引用相同的内存区域。
要真正创建不同的对象 BO1 和 BO2,您应该创建对象的副本 - 例如在业务对象类中实现 Clone() 方法。
your BO1 and BO2 are the same object
BO1 is a name that references some area in memory;
BO2 is another name that references the SAME area of memory; Session("BO") references the SAME area of memory.
To truly create different objects BO1 and BO2, you should create a copy of the object - for example implement Clone() method in your business object class.
您正在实例化两个新对象,然后将它们中的每一个设置为同一对象(即会话中的对象),因此您的行为完全符合您的预期。
顺便说一句,您可能希望考虑如果用户在选项卡中打开其中两个页面,您的页面将如何执行 - 会话中的业务对象是否会导致您出现一些问题?
You're instantiating two new objects, and then setting each of them to be the same object (i.e. the one from session), so your behaviour is exactly as you would expect.
Incidentally, you may wish to consider how your page would perform if a user opens two of these pages in a tab - will your business object in the session then cause you some problems?