Click here to Skip to main content
15,910,118 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionInternal ASP.NET threading model Pin
dojohansen11-Feb-08 7:26
dojohansen11-Feb-08 7:26 
GeneralRe: Internal ASP.NET threading model Pin
ToddHileHoffer11-Feb-08 8:15
ToddHileHoffer11-Feb-08 8:15 
GeneralRe: Internal ASP.NET threading model Pin
dojohansen11-Feb-08 23:26
dojohansen11-Feb-08 23:26 
GeneralRe: Internal ASP.NET threading model Pin
dojohansen11-Feb-08 23:31
dojohansen11-Feb-08 23:31 
GeneralRe: Internal ASP.NET threading model Pin
ToddHileHoffer12-Feb-08 1:31
ToddHileHoffer12-Feb-08 1:31 
GeneralRe: Internal ASP.NET threading model Pin
dojohansen12-Feb-08 2:12
dojohansen12-Feb-08 2:12 
GeneralRe: Internal ASP.NET threading model Pin
ToddHileHoffer12-Feb-08 2:22
ToddHileHoffer12-Feb-08 2:22 
GeneralRe: Internal ASP.NET threading model Pin
dojohansen12-Feb-08 3:00
dojohansen12-Feb-08 3:00 
We're pretty much doing the same thing. It is perfect for providing "execution context" information like who is the current user, what's the current database, and so on.

I'm going off on a tangent here, but I actually believe what we've done can be quite useful for others as well.

Our design follows this simple pattern: All instance members are private, as are all constructors. The public interface is static, but public members do nothing but find the right instance and call the corresponding instance member. References to instances are stored in the thread context, hence only one thread can access each instance, so the design is inherently thread-safe.

A snippet demonstrates the pattern implemented in C#:

public class Connection
{
   const string threadKey = "Connection";

   // Private constructor
   Connection(string cnxStr) { ... } 

   // All instance members are private...
   int transactionLevel;
   SqlTransaction transaction;
   SqlConnection sqlConn;
   ...

   // .. including methods
   void beginTransaction()
   {
      ...
   }

   // A *private* static property gets the current instance.
   Connection current
   {
     get 
     {
        Connection c = CallContext.GetData(threadKey) as Connection;
        if (c == null) 
        {
            // This is the only place in the app we need to know where 
            // connection information comes from. :)
            string cnxStr = ...;
            c = new Connection(cnxStr);
        }
        return c;
     }
   }

   // The public interface is all static, giving the illusion that there
   // is only one connection, when in fact there is one per thread.
   static public void BeginTransaction()
   {
      // Just call the corresponding instance method on "current".
      current.beginTransaction();
   }
}


We couple this with our own custom-written "Thread class" (Thread is sealed so our type is not really a thread, but it has the same interface as a thread so where our code was creating threads we just switched the type to our own) which copies the execution context of the thread that creates it, and we have a wonderfully simple to use solution with a minimum of code redundancy. (The "where do connection info come from" logic was all over the place before, and we had problems knowing when we should clone connections and when we should not, because the method using a connection would sometimes be called as part of a transaction and sometimes without one, sometimes in a thread processing a page request and sometimes in a background thread... Using the call context and our pretend-thread class solved all these problems and simplified the programming of application logic to boot!

The code it leads to is just as simple as code reading or writing a Session variable, though of course there are many session instances at any given time in the application domain. Only it is even better because no reference to the object exists outside the type itself, making it far easier to avoid memory leaks.

In the past we even had business objects that contained a reference to an instance of HttpSessionState,

myObj.Session = Session;

and this sort of terrible thing too is avoided with this design, at the expense of a larger number of lookups of course. The overhead doesn't seem to be that big though. In 1.x the HttpContext.Current also came from here (I checked with Reflector) so I'm figuring it can't be that bad.

This of course is all proprietary information Big Grin | :-D
GeneralRe: Internal ASP.NET threading model Pin
ToddHileHoffer12-Feb-08 3:16
ToddHileHoffer12-Feb-08 3:16 
QuestionRequest is not available in this context Pin
Ryno Burger11-Feb-08 6:34
Ryno Burger11-Feb-08 6:34 
GeneralRe: Request is not available in this context Pin
dojohansen11-Feb-08 7:49
dojohansen11-Feb-08 7:49 
GeneralRe: Request is not available in this context Pin
Not Active11-Feb-08 7:59
mentorNot Active11-Feb-08 7:59 
Generalneed regular expression of a strict (999) 999-9999 Format Pin
Rocky#11-Feb-08 6:05
Rocky#11-Feb-08 6:05 
AnswerRe: need regular expression of a strict (999) 999-9999 Format Pin
dilipv11-Feb-08 19:41
dilipv11-Feb-08 19:41 
GeneralRe: need regular expression of a strict (999) 999-9999 Format Pin
Rocky#11-Feb-08 19:52
Rocky#11-Feb-08 19:52 
GeneralASP.NET C# Mifare Reader Web System Pin
Tyrone Boon11-Feb-08 5:57
Tyrone Boon11-Feb-08 5:57 
GeneralCross Post Pin
led mike11-Feb-08 6:18
led mike11-Feb-08 6:18 
Generaloutlook in asp.net Pin
i gr811-Feb-08 3:50
i gr811-Feb-08 3:50 
GeneralRe: outlook in asp.net Pin
Not Active11-Feb-08 5:09
mentorNot Active11-Feb-08 5:09 
GeneralRe: outlook in asp.net Pin
i gr811-Feb-08 7:48
i gr811-Feb-08 7:48 
GeneralRe: outlook in asp.net Pin
Not Active11-Feb-08 7:54
mentorNot Active11-Feb-08 7:54 
GeneralRe: outlook in asp.net Pin
Christian Graus11-Feb-08 18:09
protectorChristian Graus11-Feb-08 18:09 
QuestionShow/Hide columns in report.rdlc localreport Pin
Member 438223211-Feb-08 3:00
Member 438223211-Feb-08 3:00 
GeneralRe: Show/Hide columns in report.rdlc localreport Pin
Paddy Boyd11-Feb-08 3:18
Paddy Boyd11-Feb-08 3:18 
GeneralRe: Show/Hide columns in report.rdlc localreport Pin
Member 438223211-Feb-08 3:57
Member 438223211-Feb-08 3:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.