我需要关心线程安全的ASP.NET与AJAX?线程、安全、ASP、NET

2023-09-10 17:17:07 作者:远得要命的你

现在的问题是,是否有可能,对于相同的会话请求被从多个线程执行的?在ASP.NET折返的方法呢?特别是,我们使用AJAX这意味着台异步请求正在发生。

The question is, is it possible that requests for the same session are executed from multiple threads? Are methods in ASP.NET reentrant? Especially we are using AJAX which means that asychronous requests are taking place.

请问这是不是意味着把周围的操作上的锁放置在会话中的对象?

Would this mean to place locks around operations on objects placed inside the session?

我知道处理静态和应用范围的变量时锁是必不可少的,但问题是是一样适用于会话对象?

I know that locks are essential when handling static and application wide variables, but the question is is the same true for session objects?

推荐答案

ASP.NET通常采用每个请求一个线程。它可以使用一个以上的线程,例如当提供异步页面,但即使如此,只有一个线程会处理请求在任何给定的时间。

ASP.NET normally uses one thread per request. It can use more than one thread, e.g. when serving asynchronous pages, but even then only one thread will be processing the request at any given time.

这是安全的但使用会话状态从多个线程,因为访问会话对象的序列化。从 MSDN :

It's safe to use the session state from multiple threads, however, because accesses to the session object are serialized. From MSDN:

如果其他网页尝试   同时访问会话状态?   在这种情况下,当前的请求   可能最终工作不一致   数据,或数据不是最新的。   只是为了避免这种情况,会话状态   模块实现读/写器   锁定机制和队列   进入状态值。一个页面,   有会话状态写入操作   举行会议作家锁   直到请求终止

What if other pages attempt to concurrently access the session state? In that case, the current request might end up working on inconsistent data, or data that isn't up to date. Just to avoid this, the session state module implements a reader/writer locking mechanism and queues the access to state values. A page that has session-state write access will hold a writer lock on the session until the request terminates.