这个“单例”应该吗? ASP.NET 应用程序中的线程安全吗?
请注意,“单例”的使用有点不常见——“对象作为单个实例可见,如 HttpContext.Current”,这与普通的“具有一个共享实例的对象”不同。
我为我的 asp.net MVC 应用程序使用单例类型的 UserContext
类。此类允许我将用户数据存储为强类型会话对象。我遇到了 这个 CodeReview 问题,想知道是否有必要关心此应用程序上下文中的线程安全。
这是我的代码的简化:
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
}
到目前为止,我还没有遇到任何锁定问题,但现在该网站的流量不是很高。我应该采用 Jon Skeet 的线程安全模型还是 IIS 为我管理它?
Note that "singleton" used in slightly uncommon sense - "object visible as single instance like HttpContext.Current" unlike normal "object with one shared instance".
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
访问
Session
已经是线程安全的了。一般来说,只要您以线程安全的方式访问静态属性中的任何共享状态,就不会有任何问题。
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.
ASP 会话状态带有同步逻辑。
如果执行的页面需要对会话状态进行写访问,则会话状态将被锁定,同一会话上的其他请求必须等待第一个请求完成。
请参阅同步对会话状态的访问。
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.
See Synchronizing Access to the Session State.