ASP.NET 中的静态属性和会话

发布于 2024-11-16 12:20:53 字数 996 浏览 2 评论 0原文

网上有很多关于此问题的帖子,但似乎没有一个提供明确的答案。我的问题是这样的。如果我声明了静态属性,仅获取/设置会话值是线程安全的还是可能会导致问题?在我的应用程序中,我向 Global.asax 添加了静态属性,作为访问某些值的中央入口点,例如,我像这样存储当前客户端:

public static string CurrentClient {
    get {
      return HttpContext.Current.Session[Constants.SESSION_CURRENT_CLIENT] as string;
    }
    set {
      HttpContext.Current.Session[Constants.SESSION_CURRENT_CLIENT] = value;
    }
}

请注意,我如何不在 get/ 中设置任何静态变量设置,我只是参考当前会话。

该应用程序已设置为在 IIS 中作为单个 Web 应用程序安装,但它将为多个不同的“实例”提供服务。基本上取决于您进入的子域,它将根据需要设置所有这些会话变量。例如:

client1.mydomain.com 将设置:

Global.CurrentClient = "client1";

client2.mydomain.com 将设置:

Global.CurrentClient = "client2";

这看起来应该工作正常并且是线程安全的,并且两个子域不会互相绊倒,因为它们每个都应该有唯一的会话,但那就是到底发生了什么。由于某种原因,我使用 CurrentClient="client2" 收到对 client1.mydomain.com 的请求。

这帮人到底是怎么回事?

There are several posts about this online but none seem to provide a definitive answer. My question is this. If I have static properties declared that solely get/set Session values is that thread safe or will it potentially cause problems? In my app I have added static properties to my Global.asax to serve as a sort of central entry point for accessing certain values, for example I store the current client like this:

public static string CurrentClient {
    get {
      return HttpContext.Current.Session[Constants.SESSION_CURRENT_CLIENT] as string;
    }
    set {
      HttpContext.Current.Session[Constants.SESSION_CURRENT_CLIENT] = value;
    }
}

Note how I am not setting any static variables in my get/set, I am merely referencing the current session.

The application is setup so that it is installed as a single webapp in IIS but it will service multiple different 'instances'. Basically depending on what subdomain you come in on, it will then set all these Session variables as required. So for example:

client1.mydomain.com will set:

Global.CurrentClient = "client1";

client2.mydomain.com will set:

Global.CurrentClient = "client2";

This seems like it should work fine and be thread safe and the two subdomains will not trip over one another because they should each have unique sessions but that's exactly what seems to be happening. I get requests to client1.mydomain.com using CurrentClient="client2" for some reason.

What's going on here gang?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

千寻… 2024-11-23 12:20:53

你似乎有严重的静电恐惧症。你不应该仅仅因为不明白自己在做什么而听信那些传播 FUD 的人的言论。

静态属性本质上是静态方法。它们本身不存储任何状态。自动属性当然是一个例外,但您似乎没有使用它们。

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

是什么导致您的会话变得“混乱”,您确定会话 cookie 设置在正确的级别吗?如果您将其设置在 mydomain.com 级别,它将在所有子域之间共享。另外,您确定有必要将这些内容存储在会话中吗?在每个请求中将当前域与您的客户列表进行比较不是最简单的吗?

You seem to have a bad case of static-phobia. You shouldn't listen to people who spread FUD just because they don't understand what they're doing.

Static properties are essentially static methods. They do not store any state by themselves. Auto-properties are of course an exception, but you don't seem to be using them.

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

What comes to your sessions getting "mixed up", are you sure the session cookie is being set at the correct level? If you set it at the mydomain.com level, it's going to be shared across all the subdomains. Also, are you sure it's even necessary to store this stuff in the session? Wouldn't it be the easiest to just compare the current domain with your list of clients on every request?

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