开发者

Do I need to care about thread-safety in ASP.NET with 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 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.

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.


The question is, is it possible that requests for the same session are executed from multiple threads?

If you use HTTP Session inside your page, then requests for the same session will be queued by the ASP.NET engine. They will never run in parallel. So for example if you send multiple AJAX requests to some ASP.NET page which uses session those requests will execute sequentially.

If you don't use Session then multiple concurrent requests will run in parallel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜