ASP.NET 会话中的共享方法

发布于 2024-10-19 01:47:05 字数 1339 浏览 1 评论 0原文

作为上一个问题的后续,我问了 "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故笙诉离歌 2024-10-26 01:47:05

在您的代码示例中,变量 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.

非要怀念 2024-10-26 01:47:05

Shared 方法的唯一问题是共享状态

如果您使用Shared字段或Static变量,它将在请求之间共享,并导致麻烦。

但是,不使用任何外部状态或共享对象的 Shared 方法可以正常工作。

The only issue with Shared methods is shared state.

If you use a Shared field or Static 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.

临风闻羌笛 2024-10-26 01:47:05

即使正在运行相同的方法,它也是在两个不同的上下文中运行。该方法的任何本地变量(包括 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文