HttpContext in static functions.

S

Steve

If I call HttpContext from within a static function will I always be
guaranteed to get the current HttpContext for the request/code that
called the static function? Is there ever a chance that a user might get
another user's request context instead?

Thanks,
Steve
 
P

Paul Glavich [MVP - ASP.NET]

No, but you will have to make sure your code is thread safe and can handle
concurrency issues, otherwise it might seem like to context is getting mixed
up, but its more than likely your code. If you really need the static
function then so be it, but to get around the concurrency issue, you should
really use an instance class. Much easier.
 
S

Steve

Paul said:
No, but you will have to make sure your code is thread safe and can handle
concurrency issues, otherwise it might seem like to context is getting mixed
up, but its more than likely your code. If you really need the static
function then so be it, but to get around the concurrency issue, you should
really use an instance class. Much easier.

Sorry for the confusion. Since I asked two questions in my post, I'm not
sure which one the "No" is for. Are you saying that the HttpContext will
be good and proper, and that I just need to make sure that any other
code that I have around it obeys the necessary requirements for working
in a static setting(no globals, locking collections, etc...)?

Thanks,
Steve
 
R

Rick Strahl [MVP]

Steve,

HttpContext.Current will almost always return the current request context.
<g>

The exception is when you work with work threads. HttpContext is bound to
the incoming ASP.NET worker thread and the Context will be available on that
thread. Outside of that you might have to store away an instance of the
context to maintain access to it.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
 

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,794
Messages
2,569,641
Members
45,353
Latest member
RogerDoger

Latest Threads

Top