如果这个“单例”在ASP中是线程安全的。网络应用程序?

时间:2022-10-22 10:15:07

Note that "singleton" used in slightly uncommon sense - "object visible as single instance like HttpContext.Current" unlike normal "object with one shared instance".

注意,“单例”在不太常见的意义上使用——“对象”作为单个实例可见,如HttpContext。当前“不像普通的”对象与一个共享实例。

I make use of a singleton type of UserContext class for my asp.net MVC applications. This class allows me to store user data as a strongly-typed session object. I ran across this CodeReview question and wondered if it was necessary to be concerned about thread safety in this application context.

我在asp.net MVC应用程序中使用了一个单一类型的UserContext类。这个类允许我将用户数据存储为强类型的会话对象。我遇到了这个CodeReview问题,我想知道是否有必要在这个应用程序上下文中关注线程安全性。

Here's a simplification of my code:

下面是我的代码的简化:

public class UserContext
{
    private UserContext()
    {
    }

    public static UserContext Current
    {
        get
        {
            if (HttpContext.Current.Session["UserContext"] == null)
                BuildUserContext();

            return (UserContext)HttpContext.Current.Session["UserContext"];
        }
    }

    private static void BuildUserContext()
    {
        if (!user.Identity.IsAuthenticated) return;

        var uc = new UserContext { IsAuthenticated = true };

        // ...snip...
        // Set up user data

        // Save it into the session
        HttpContext.Current.Session["UserContext"] = uc;
    }


    #region Class members
    public bool IsAuthenticated { get; internal set; }
    public string Name { get; internal set; }
    // ...snip...
    // Other properties

    public void Refresh()
    {
        BuildUserContext();
    }

    public void Flush()
    {
        HttpContext.Current.Session["UserContext"] = null;
    }
    #endregion
}

I haven't had any locking issues so far, but right now the site is not very high traffic. Should I adopt Jon Skeet's thread-safe model or does IIS manage that for me?

到目前为止,我还没有遇到任何锁定问题,但是现在这个站点的流量不是很大。我应该采用Jon Skeet的线程安全模型,还是IIS为我管理这个模型?

2 个解决方案

#1


2  

Access the Session is already Thread safe.

访问会话已经是线程安全的。

In general as long as you access any shared state in your static properties in a thread-safe manner, you won't have any problems.

通常,只要您以线程安全的方式访问静态属性中的任何共享状态,就不会有任何问题。

#2


1  

ASP session state comes with a synchronizing logic. If the executed page needs write access to the session state, the session state is locked and other request on the same session has to wait until the first one finishes.

ASP会话状态带有同步逻辑。如果执行的页面需要对会话状态进行写访问,则会话状态被锁定,同一会话上的其他请求必须等到第一个会话完成时。

See Synchronizing Access to the Session State.

请参见对会话状态的同步访问。

#1


2  

Access the Session is already Thread safe.

访问会话已经是线程安全的。

In general as long as you access any shared state in your static properties in a thread-safe manner, you won't have any problems.

通常,只要您以线程安全的方式访问静态属性中的任何共享状态,就不会有任何问题。

#2


1  

ASP session state comes with a synchronizing logic. If the executed page needs write access to the session state, the session state is locked and other request on the same session has to wait until the first one finishes.

ASP会话状态带有同步逻辑。如果执行的页面需要对会话状态进行写访问,则会话状态被锁定,同一会话上的其他请求必须等到第一个会话完成时。

See Synchronizing Access to the Session State.

请参见对会话状态的同步访问。