c# - C#中的无锁 session 是什么?

标签 c# asp.net session

我知道 C# 中的 session 以及如何定义它们。但是今天我听到了一个术语Lock-free sessions。我用谷歌搜索但没有得到与我的问题完全匹配的答案。谁能解释一下 C# 中的无锁 session 以及如何为它们编写代码?

最佳答案

以下是来自 msdn 的部分内容并发请求和 session 状态

部分

Access to ASP.NET session state is exclusive per session, which means that if two different users make concurrent requests, access to each separate session is granted concurrently. However, if two concurrent requests are made for the same session (by using the same SessionID value), the first request gets exclusive access to the session information. The second request executes only after the first request is finished. (The second session can also get access if the exclusive lock on the information is freed because the first request exceeds the lock time-out.) If the EnableSessionState value in the @ Page directive is set to ReadOnly, a request for the read-only session information does not result in an exclusive lock on the session data. However, read-only requests for session data might still have to wait for a lock set by a read-write request for session data to clear.

因此,只要并发请求带有相同的 sessionId,它就会进入独占锁。要创建无锁 session ,您只需按照上述 MSDN 文档将 EnableSessionState 设置为 ReadOnly。这称为无锁 session

注意:当您将 EnableSessionState 指定为 ReadOnly 时。 asp.net 不会在 session 上获取任何独占锁,最终它还会将 session 设为该页面的只读。

这是关于另一个堆栈溢出线程上的 asp.net 中的 session 锁的非常好的讨论:- link

关于c# - C#中的无锁 session 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27957295/

相关文章:

c# - 抛出 HttpResponseException 会导致未处理的异常

asp.net - 三层 Asp.Net 应用程序中的异常处理

.net - 什么是仅为 session 启用 Mini-profiler 的简单方法?

javascript - 金特 "unexpected reserved word"

c# - 在 Direct X 11 游戏中挂接 Direct X Endscene

C# IList更新问题

javascript - 在元素上设置验证样式

session - XMPP 可以在 NAT 环境中工作吗?

java - 如何在 Spring Web 服务中管理 session ?

c# - NLog自定义目标LogCat