ASP.NET 会话中的共享方法
作为上一个问题的后续,我问了 "ASP.Net 架构特定共享/静态函数”
我仍在努力理解在 ASP.NET 中使用共享方法的含义。
例如,让我们采用以下代码。
Public Shared Function GetCategoryByID(ByVal CategoryID As Guid) As Category
If Not CategoryID.Equals(Guid.Empty) Then
Dim res As New Category
Using sqlConn As New SqlConnection(My.Settings.ConnectionString)
sqlConn.Open()
Using dr As IDataReader = CategoryDataLayer.GetCategoryByIDQuery(sqlConn, CategoryID)
Return CType(BaseDataLayer.FillObject(res, dr), Category)
End Using
End Using
Else
Return Nothing
End If
End Function
现在我想象客户端 1 使用 guid {A20E625F-2333-4742-BFD9-05BE7649222D} 从其会话中连接并运行此方法 GetCategoryByID()。
现在让我们假设该进程即将执行以下行。
Using dr As IDataReader = CategoryDataLayer.GetCategoryByIDQuery(sqlConn, CategoryID)
此时,客户端 2 运行相同的共享方法,但使用 guid {6D806B82-FC7F-4124-AFB9-45E2689BC9F5}。
此时 CategoryID 是否不会变为 {6D806B82-FC7F-4124-AFB9-45E2689BC9F5} 并因此干扰客户端 1,因为考虑到这是共享方法,现在 CategoryID 已更改?
有人可以澄清一下吗?
PS:我确实对本质上是重复的帖子表示歉意,但回想起来,我觉得原始帖子中的答案不够明确(至少对我来说)。也许我也不够具体......
As a followup to the previous question I have asked "ASP.Net Architecture Specific to Shared/Static functions"
I am still struggling to understand the implications of using shared methods in ASP.NET.
So for example let us take the following code.
Public Shared Function GetCategoryByID(ByVal CategoryID As Guid) As Category
If Not CategoryID.Equals(Guid.Empty) Then
Dim res As New Category
Using sqlConn As New SqlConnection(My.Settings.ConnectionString)
sqlConn.Open()
Using dr As IDataReader = CategoryDataLayer.GetCategoryByIDQuery(sqlConn, CategoryID)
Return CType(BaseDataLayer.FillObject(res, dr), Category)
End Using
End Using
Else
Return Nothing
End If
End Function
Now I imagine client 1 connecting and running this method GetCategoryByID() from their session with the guid {A20E625F-2333-4742-BFD9-05BE7649222D}.
Let us now say that for example the process is about to execute the following line.
Using dr As IDataReader = CategoryDataLayer.GetCategoryByIDQuery(sqlConn, CategoryID)
At this point client 2 runs the same shared method but with the guid {6D806B82-FC7F-4124-AFB9-45E2689BC9F5}.
Does CategoryID not at this point become {6D806B82-FC7F-4124-AFB9-45E2689BC9F5} and therefor interfere with client 1 because now CategoryID has changed given that this is a shared method?
Could someone please clarify?
PS: I do apologize for what is essentially a duplicate post but in retrospect I don't feel the answer in the original post was clear enough (at least for me). Perhaps I wasn't specific enough either...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的代码示例中,变量
res
是共享方法的局部变量。没有人,甚至另一个共享方法都不能触及该变量。它会在方法的生命周期内存在,然后消失。CategoryId
完全相同,它是一个无法从外部触及的局部变量。VB 还有另一个称为
静态
的概念,它与 C# 版本的静态有很大不同,这可能会导致一些混乱。In your code sample, the variable
res
is a local variable of the shared method. No one, not even another shared method can touch that variable. It will exist for the lifetime of the method and then its gone.CategoryId
is the exact same, its a local variable that cannot be touched from outside.VB has another concept called
Static
which is very different than the C# version of static which can cause some confusion.Shared
方法的唯一问题是共享状态。如果您使用
Shared
字段或Static
变量,它将在请求之间共享,并导致麻烦。但是,不使用任何外部状态或共享对象的
Shared
方法可以正常工作。The only issue with
Shared
methods is shared state.If you use a
Shared
field orStatic
variable, it will be shared across requests, and cause trouble.However, a
Shared
method that doesn't use any external state or shared objects will work fine.即使正在运行相同的方法,它也是在两个不同的上下文中运行。该方法的任何本地变量(包括 CategoryId 参数)都不会共享。
Even though the same method is being run, it is being run in two different contexts. Any variables local to that method (including the CategoryId parameter) are not shared.