March 9, 2021
Hot Topics:

The ASP.NET 2.0 Provider Model

  • By Dino Esposito
  • Send Email »
  • More Articles »
by Dino Esposito of Wintellect

Most applications need to save and retrieve data to and from persistent media. In ASP.NET, a good example of this kind of persistent data is the session state. Developers write applications that use the session state, but neither end-users nor developers really know where or how the session state is held. ASP.NET developers can control the location of the data to some extent, however, by instructing the ASP.NET runtime to save and retrieve the state from local memory, memory of an external process, or even a SQL Server table.

In spite of the various storage options, the programming interface to the session state remains the same (the web.config file). For example, to move the session state from the local memory (the ASP.NET cache) to a SQL Server table, all you do is modify an attribute in the web.config file. You don't need to recompile or even touch the code. This article reveals the magic behind this behavior, which the ASP.NET 2.0 provider model provides.

ASP.NET 1.x Session State Module

When the session state HTTP module needs to retrieve the session state for an ongoing request, it reads the information in the web.config file and figures out which state manager component is in charge of providing the session state. A state manager is an ASP.NET internal component that knows how to retrieve the collection of session state items for a particular request. State managers differentiate themselves by the storage media they use to persist data, yet these .NET classes all implement the same interface, IStateClientManager.

The session state module figures out the name of a state manager class based on the session mode attribute in the web.config file. Next, it uses a switch statement to instantiate the right component, as in the following pseudo-code:

int mode = sessionStateConfig.Mode;
switch (mode)
   case 1:
      this.stateManager = new InProcStateClientManager();
   case 2:
      this.stateManager = new OutOfProcStateClientManager();
   case 3:
      this.stateManager = new SqlStateClientManager();

All three state client manager classes implement the same interface, thus providing the same functionality to the HTTP module.

Although this model is effective and fully functional, it hardly is extensible. What if you want to store your session state in, say, an Oracle database?

Page 1 of 3

This article was originally published on July 14, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date