in

ASP.NET Weblogs

Li Chen's Blog

Sharing session between ASP Classic and ASP.NET using ASP.NET Session state server

The different session state used by ASP Classic and ASP.NET is the most significant obstacle in ASP and ASP.NET interoperability. I started an open source project called NSession with the goal to allow ASP Classic to access ASP.NET out-of-process session stores in the same way that ASP.NET accesses them, and thus share the session state with ASP.NET.

How does ASP.NET access session state?

The underpinning of ASP.NET session state was discussed in detail by Dino. ASP.NET retrieves the session state from the session store and de-serializes it into an in-memory session dictionary at the beginning of page request. If the page requests for a read-writable session, ASP.NET will lock the session exclusively until the end of the page request, and then serialize the session state into the session store and release the lock. If a page requests for a read-only session, session state is retrieved without an exclusive lock.

We intend to mimic the behavior of ASP.NET in ASP Classic.

How does NSession work?

You need to instantiate one of the COM objects in your ASP classic page before it accesses session state, either:

set oSession = Server.CreateObject("NSession.Session")

or

set oSession = Server.CreateObject("NSession.ReadOnlySession")

If you instantiate NSession.Session, the session state in the session store will be transferred to the ASP Classic session dictionary, and an exclusive lock will be placed in the session store. You do not need to change your existing code that accesses the ASP Classic session object. When NSession.Session goes out of scope, it will save the ASP Classic session dictionary back to the session store and release the exclusive lock.

If you have done with the session state, you can release the lock early with

set oSession = Nothing

If you instantiate NSession.ReadOnlySession, the session state in the session store will be transferred to the ASP Classic session dictionary but no locks will be placed.

Installation

You may download the binary code from the Codeplex site. There are 2 DLLs to register. The first dll NSession.dll is a .net framework 4.0 dll and it is independent of 32 or 64 bit environment. Since it is accessed by ASP Classic, it needs to be registered as a COM object using RegAsm. It also needs to be place in global assembly cache using gacutil. You need .NET framework 4 on the machine but you can run any version of ASP.NET as the DLL is not loaded into the same AppDomain as the ASP.NET application. The reason that I chose .NET framework 4.0 is that I can use the C# dynamic feature to cut down the reflection.
The second dll NSessionNative.dll is a C++ dll and is platform dependent. You need to register the appropriate version depending on whether you are running the 32 or 64 bit application pool. The reason that we need a C++ dll is that we need deterministic finalization to serialize the session at the end of page request. We chose C++ over VB6 because it can create both 32 bit and 64 bit COM objects.

As this is still a beta ware, I suggest that you use it like any pre-release software. Please report any bug and issue to the project site forum.

Future works

As this time, I have only implemented the code to access ASP.NET state server. The following are some of the ideas that I have in mind. Throw in yours in the Issue Tracker at the project site.

1. Support Sql Server session store.

2. Optimization. ASP.NET caches some configuration and objects. NSession could use the idea too.

3. Configuration setting to allow clearing of ASP Classic session dictionary and the end of page request.

4. Implement a filter mechanism. The ASP.NET may store some objects that do not make sense for ASP Classic and vice versa. A filter mechanism can be used to filter out these objects.

Published Oct 30 2011, 06:30 AM by dotneteer
Filed under: ,

Comments

 

Furtif said:

Great project, I never find a proper solution to share Classic ASP session with ASP.NET and your solution looks really good and fast.

Do you think there is a way to use Server.Execute to call an aspx page ?

October 31, 2011 10:01 AM
 

dotneteer said:

@Furtif, I think using Server.Execute to call an asps page from asp directly would be fairly difficult. The closest that I can come to is to create an object to emulate the intrinsic Server object. It will make a web request to ASP.NET using the same cookie and write the web response into the ASP classic response. You will have to instantiate this emulator object and call its Execute method.

October 31, 2011 12:56 PM
 

Bertrand Le Roy said:

Cool stuff. About 8 years ago, I did something similar by hosting the active scripting host COM object in ASP.NET and handing it the ASP.NET session as its session object. This way, the classic ASP scripts run inside ASP.NET and have no idea that they are not using the native session.

October 31, 2011 6:10 PM
 

dotneteer said:

Yes, Bertrand. I did not notice your work at www.uop.edu.jo/.../ASP2ASPNET.pdf and that inspired me to my very experimental ASP Classic Compiler project (aspclassiccompiler.codeplex.com). This time, I am taking a step back to come out with something with lower risk so we can immediately use in our own production applications.

November 1, 2011 1:43 AM

Leave a Comment

(required)  
(optional)
(required)  
Add