全局范围内的 .NET 会话替代方案

发布于 2024-10-19 02:38:17 字数 586 浏览 3 评论 0原文

简单问题:是否有一个“每用户”数据存储对象(类似于Session),我可以在全局范围内存储数据(类似于HttpRuntime.缓存)?几乎就像 SessionHttpRuntime.Cache 生了一个孩子。

完整背景:我有一个 ASP.NET 网站,最初是为单线程编写的。现在我更改了它,以便某些操作将生成后台线程,并且浏览器轮询服务以获取状态更新。

我遇到的问题是某些数据存储在 HttpContext.Session[] 对象中(例如成员身份验证令牌)。这些数据对于每个用户来说都是唯一的,并且可以被后台线程访问。会话不可用于后台线程。

我知道 HttpRuntime.Cache ,但这需要微观管理来分割用户并在会话过期的同时使其过期。另一方面,Session 会在我也想要的正确时间自动使这些东西过期,并且已经被 SqlMembershipProvider 之类的东西使用。

我的问题是,是否有一些行为类似于 Session 但存在于全局范围内的东西?

Quick question: Is there a "per-user" data storage object (similar to Session) that I can store data in the global scope (similar to HttpRuntime.Cache)? Almost as if Session and HttpRuntime.Cache had a baby.

Full Background: I have a ASP.NET website that was originally written for a single thread. Now I changed it so that certain actions will spawn a background thread and the browser polls a service to get status updates.

The problem I am having with this is that certain pieces of data are stored into the HttpContext.Session[] object (membership authentication token, for example). These pieces of data need to be unique to each user and accessible to the background thread. Session is not available to the background thread.

I am aware of HttpRuntime.Cache but that would require micromanagement to segment out the users and to expire it at the same time the session is expired. Session, on the other hand, automatically expires this things at the right times that I want it too and is already used by things like the SqlMembershipProvider.

My question is, is there something that behaves similar to the Session but exists in the global scope?

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

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

发布评论

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

评论(3

笑脸一如从前 2024-10-26 02:38:17

我认为没有像您需要开箱即用的东西。不过,我会执行以下操作:

  1. 使用应用程序缓存
  2. 将键设为用户 ID 或唯一标识符
  3. 在用户的值中存储字典或某些对象列表。用它来存储您需要的所有数据。
  4. 如果您认为可能与用户唯一标识符(例如域等)发生冲突,请考虑在用户 ID 上添加所有内容
  5. 确保对缓存数据设置类似于会话的过期时间(例如滑动)

I don't think there is anything like you need out of the box. I would do the following though:

  1. User the application cache
  2. Make the key the user ID or unique identifier
  3. Store a Dictionary or some object list in the value for the user. Use this to store all the data you require.
  4. Consider all prepending something on the user ID if you think there could be a conflict with the user unique identifier (eg domain etc)
  5. Make sure to set an expiry on the cached data similar to the session (eg sliding)
我的鱼塘能养鲲 2024-10-26 02:38:17

尝试将 HttpContext.Current 对象传递给后台线程上的方法。您应该能够通过 currentContext.Session 从后台线程访问会话,假设 currentContext 是传入的 HttpContext 参数。

请参阅此关于如何从多个线程安全访问 HttpContext 对象的博客文章

Try passing the HttpContext.Current object to the method on your background thread. You should be able to access the session from the background thread through currentContext.Session assuming currentContext is the HttpContext parameter that was passed in.

See this blog post on how to safely access the HttpContext object from multiple threads.

爱你是孤单的心事 2024-10-26 02:38:17

不会。

因为当应用程序池重新启动时,所有后台活动都会消失,我建议考虑将用户的状态移动到您自己的数据库或外部存储中。不幸的是,您将失去自动会话管理的好处(滑动过期),但如果您需要后台活动,它会工作得更好 - 即,如果稍后需要,您将能够将活动从 IIS 进程移至单独的进程/机器。

No.

Since when application pool restarts all backgound activity die I suggest to think about moving user's state to your own database or external storage. Unfortunately you'll lose automatic session management benifits (sliding expiration), but if you need backgound activity it will work better - i.e. you'll be able to move your activity out of IIS process to separate process/machine if needed later.

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