Is access to the Session thread-safe?

C

Chris Newby

When accessing, for example, an object stored in the session such as:

Session[ "MyObject" ].MyProperty = "Some Value";

Is access to MyObject thread-safe?
 
S

Scott Allen

Hi Chris:

Here are some details:

ASP.NET does manage access to session state with a ReaderWriterLock.
The default is to take a writer lock, which serializes requests coming
in on the same session (using EnableSessionState="ReadOnly" takes a
reader lock and does allow concurrent request processing on a
session).

In this example, you are not really modifying session state, but
modifying an object through a reference stored in session state. If
anyone else has a reference to the same object the code is only thread
safe if MyObject is thread safe.

Know what I mean?
 
C

Chris Newby

Yeah, makes perfect sense.

Could you possibly elaborate a little more on exactly when ASP.NET creates
and releases the ReaderWriter Lock?


Scott Allen said:
Hi Chris:

Here are some details:

ASP.NET does manage access to session state with a ReaderWriterLock.
The default is to take a writer lock, which serializes requests coming
in on the same session (using EnableSessionState="ReadOnly" takes a
reader lock and does allow concurrent request processing on a
session).

In this example, you are not really modifying session state, but
modifying an object through a reference stored in session state. If
anyone else has a reference to the same object the code is only thread
safe if MyObject is thread safe.

Know what I mean?

--
Scott
http://www.OdeToCode.com/blogs/scott/


When accessing, for example, an object stored in the session such as:

Session[ "MyObject" ].MyProperty = "Some Value";

Is access to MyObject thread-safe?
 
S

Scott Allen

When the SessionStateModule catches the AcquireRequestState event
during request processing. This happens before control reaches the
ASPX page handler.
 
C

Chris Newby

Thanks Scott, very helpful//

Scott Allen said:
When the SessionStateModule catches the AcquireRequestState event
during request processing. This happens before control reaches the
ASPX page handler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top