473,322 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Using CallContext For Request-Specific Storage

Hello,

Is it appropriate to use the CallContext class for storing
request-specific data? I have a set of business objects that
I'm building. Each class contains a hashtable that maps from a key to
an object. Whenever a constructor is called, the hashtable is
consulted first to see if an object with that key already exists. If
it doesn't, the object is pulled from the database and put inside of
the hashtable. I'd like the hashtables to be specific to an execution
context -- for instance, if two different web requests create a Person
object with an ID of 100, I'd like them to get 2 different objects.
But within one executing web request, if a Person object with an ID of
100 is created twice, I'd like the same object to be returned both
times.

The business objects might be used outside of a web context, so I
can't rely on the HttpContext.Items collection -- would CallContext be
an appropriate alternative? It resides in the
System.Runtime.Remoting.Messaging namespace, which gives me pause.
I've copied below the two relevant methods for fetching and storing an
object. Thanks in advance for any help!

public static Person GetByKey(int id)
{
object o = CallContext.GetData("PersonMap");
if (o != null)
{
Hashtable map = (Hashtable)o;
return map[id] as Person;
}
else
return null;
}
public static void AddToMap(Person person)
{

if (CallContext.GetData("PersonMap") == null)
{
Hashtable map = new Hashtable();
map.Add(person._id, person);
CallContext.SetData("PersonMap", map);
}
else
{
Hashtable map = (Hashtable)CallContext.GetData("PersonMap");
map.Add(person._id, person);
}
}
Nov 18 '05 #1
5 3032
no, as the CallContext is tied to a thread. if your business object is
called from an asp.net page, its not guaranteed that it will be called by
the same thread thoughout the processing of a single page request (see
asp.net thread agility).

a better design would be to use instance data in you business object, and
not use static methods. also the code would be simpler and faster.
-- bruce (sqlwork.com)

"Pham Nguyen" <sh*********@yahoo.com> wrote in message
news:f6**************************@posting.google.c om...
Hello,

Is it appropriate to use the CallContext class for storing
request-specific data? I have a set of business objects that
I'm building. Each class contains a hashtable that maps from a key to
an object. Whenever a constructor is called, the hashtable is
consulted first to see if an object with that key already exists. If
it doesn't, the object is pulled from the database and put inside of
the hashtable. I'd like the hashtables to be specific to an execution
context -- for instance, if two different web requests create a Person
object with an ID of 100, I'd like them to get 2 different objects.
But within one executing web request, if a Person object with an ID of
100 is created twice, I'd like the same object to be returned both
times.

The business objects might be used outside of a web context, so I
can't rely on the HttpContext.Items collection -- would CallContext be
an appropriate alternative? It resides in the
System.Runtime.Remoting.Messaging namespace, which gives me pause.
I've copied below the two relevant methods for fetching and storing an
object. Thanks in advance for any help!

public static Person GetByKey(int id)
{
object o = CallContext.GetData("PersonMap");
if (o != null)
{
Hashtable map = (Hashtable)o;
return map[id] as Person;
}
else
return null;
}
public static void AddToMap(Person person)
{

if (CallContext.GetData("PersonMap") == null)
{
Hashtable map = new Hashtable();
map.Add(person._id, person);
CallContext.SetData("PersonMap", map);
}
else
{
Hashtable map = (Hashtable)CallContext.GetData("PersonMap");
map.Add(person._id, person);
}
}

Nov 18 '05 #2
"bruce barker" <no***********@safeco.com> wrote in message news:<uV**************@TK2MSFTNGP12.phx.gbl>...
no, as the CallContext is tied to a thread. if your business object is
called from an asp.net page, its not guaranteed that it will be called by
the same thread thoughout the processing of a single page request (see
asp.net thread agility).

a better design would be to use instance data in you business object, and
not use static methods. also the code would be simpler and faster.


These object maps need to be globally visible throughout a request
(but expire at the end of it). I don't see how I would do this with
instance data, unless I pass an object containing the maps around
everywhere I might need to do a lookup.

I've been thinking of putting the maps in a separate class from the
business objects, one that tests whether the code is executing from
the web or not, and uses HttpContext and CallContext to store the
maps, respectively...
Nov 18 '05 #3
Pham Nguyen wrote:
Pham Nguyen wrote:
Is it appropriate to use the CallContext class for storing
request-specific data?

"bruce barker" <no***********@safeco.com> wrote:

no, as the CallContext is tied to a thread. if your business
object is called from an asp.net page, its not guaranteed
that it will be called by the same thread thoughout the
processing of a single page request (see asp.net thread
agility).


Actually, CallContext does appear to be appropriate per Pham's request.

Here's fact that contradicts Bruce's assertion: HttpContext.Current is
guaranteed to be constant during the execution of an asp.net page,
regardless of whether the thread executing the page changes under the
hood (as Bruce states can happen). HOWEVER, HttpContext.Current itself
uses CallContext (you can use ildasm.exe or reflector to discover
this).

CallContext seems to be what Pham is looking for - something that "like
HttpContext.Current.Items, but doesn't require HttpContext".
Alternatively, you could state the requirement as "a ThreadStatic
storage field that is reset to null when threads are recycled in the
thread pool." (threadstatics retain their value when they are
recycled).

As an aside, the documentation for CallContext is pretty slim, so it
seems like this behavior (which is quite useful) might change in future
releases of the framework. If so, I would just pull out Reflector
again to find out what HttpContext.Current is using...

Nov 19 '05 #4
Hi Finn:

Here's fact that contradicts Bruce's assertion: HttpContext.Current is
guaranteed to be constant during the execution of an asp.net page,
regardless of whether the thread executing the page changes under the
hood (as Bruce states can happen). HOWEVER, HttpContext.Current itself
uses CallContext (you can use ildasm.exe or reflector to discover
this).


It is dangerous to rely on hidden implementation details like this.
Context.Items is the place for request specific data. Relying on
CallContext or any other form of thread local storage could be a
problem in future version of the framework.

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #5
Hi Scott,
It is dangerous to rely on hidden implementation details like this.
Context.Items is the place for request specific data. Relying on
CallContext or any other form of thread local storage could be a
problem in future version of the framework.


You're right - it is a risky thing to do. However, Pham needs
something that can work outside of a web context, so
HttpContext.Current isn't guaranteed to be non-null (and thus using
Context.Items isn't a possibility).

Faced with that requirement, the question becomes "how can I get
something _like_ HttpContext.Current?" An obvious answer is "figure
out how HttpContext does it" - you just might learn something along the
way.

I don't think this approach is _necessarily_ risky - after all, what
better way to learn about the framework then to read the code of some
object that is very close to your requirements? What makes it risky in
this situation is that the documentation of CallContext is so slim that
one cannot tell if the behavior is guaranteed or incidental.

Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: José Joye | last post by:
Hello, What is the way to get the Network name of the client using remoting a NT Service exposes? Thanks, José
0
by: A Rothberg | last post by:
Have some questions about the behavior of CallContext and visibility to the CallContext data. Can someone please confirm (or deny) that the observed behaviors are by design and will not be...
0
by: José Joye | last post by:
I'm playing around with the "CallContext" class to pass around extra information between my client and Server applications (implemented with Remoting). My Server is a SAO-singlecall. If I try...
2
by: José Joye | last post by:
I'm playing around with the "CallContext" class to pass around extra information between my client and Server applications (implemented with Remoting). My Server is a SAO-singlecall. If I try...
3
by: Brian Birtle | last post by:
**** A CHALLENGE TO THE GURUS - refute the statement "It's impossible to build a file upload progress meter using ASP.NET" **** First person to prove me wrong gets "All Time .NET Programming GOD"...
3
by: Patrick Fogarty | last post by:
I am programming what is to be a web service client that will use an HTTP-POST to request and retrieve data. The remote server (written in java for what it's worth) requires basic authentication...
1
by: Tommaso Caldarola | last post by:
If I call System.Runtime.Remoting.CallContext.SetData() and then I call System.Runtime.Remoting.CallContext.GetData() from a different thread I get null, if the thread indeed is the same I get...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
5
by: Charles Zhang | last post by:
I am creating dynamic web pages according to the user inputs from a web page. For instance, a user typed in a text, I would want the text being the source code of the dynamic page. My...
8
by: inpuarg | last post by:
I 'm developing a c# (.net 2.0) windows forms application and in this application i want to connect to a java servlet page (HTTPS) (which is servlet 2.4 and which may be using Web Based SSO Sun...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.