HttpContext.Current Property

  • Thread starter Francois Malgreve
  • Start date
F

Francois Malgreve

hello guys,

I have some helper class in my ASP.NET pplication who basically contains
static methods. I used them as helper methods to do small jobs that I can
use at many places in my code.

As those classes methods are static, they are never associated with any
instance, then not assoicated with any HttpRequest neither.
Nevertheless in some case I ended up myself passing the Session object to my
static methods to be able to get some session related information. But then
I found the sstatic property HttpContext.Current which returns me an
HttpContext object. In which I can find all my objects related to a session,
request and response. Miracle.

Now my question is how can a static property point you to the right
request/response/session? How can this static HttpContext.Current property
point you to the right HttpContext object? Is ASP.NET mono threaded the
Context is attached some how to the thread? Or if it is multithreaded (a
thread per request I would assume) is the context attached to the thread
serving a particular request? I need to understsand the underlying mechanism
to be sure that I won't have any bug later on.

Thank you to give me any information or point me to any doc explaining the
mechanism of that static property.

Best regards,

Francois
 
M

Martin

Now my question is how can a static property point you to the right
request/response/session? How can this static HttpContext.Current property
point you to the right HttpContext object? Is ASP.NET mono threaded the
Context is attached some how to the thread? Or if it is multithreaded (a
thread per request I would assume) is the context attached to the thread
serving a particular request? I need to understsand the underlying mechanism
to be sure that I won't have any bug later on.

It seems you are misinterpreting the Static keyword. Static is not constant,
it just means there is no context available for the HttpContext object
itself when the property's get method is being processed and that you may
therefor invoke it on the HttpContext class as you may on a HttpContext
instance. Static does not mean you will always get the same value, a static
property could return the current time for that matter (like DateTime.Now).

So HttpContext.Current gives you a HttpContext instance which wraps stuff
related to the current page request. This may be a diffenrent instance for
each page request or the same object for each request within the same
session that is being updated by ASP.NET on each request, I don't know. The
Current property's get method just fetches and returns an appropriate
HttpContext for you.
 
S

Scott Allen

Although I cannot find it officially documented, I'd bet the
HttpContent.Current property pulls data stored in thread local storage
(TLS). TLS gives each thread a location to store thread specific data.
In .NET TLS is setup using the ThreadStaticAttribute. TLS would allow
the ASP.NET runtime to associate a request context with each thread
while remaining multithreded.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top