Share Session state Between Classic ASP and ASP. NET (2)

Source: Internet
Author: User
Tags array exit config constructor goto include split string back
Asp.net|session ASP Implementation
The native ASP session can only store sessions data in memory. In order to store the session data to SQL Server, a custom microsoft®visual basic®6.0 COM object are written to manage th E session state instead of using the native session object. This COM object is instantiated in the beginning of each WEB request and reload to the session data from SQL Server. When the ASP script was finished, this object would be terminated and the session state'll be persisted back to SQL Server .

The primary purpose of the Visual Basic 6 COM Session object was to provide access to the Microsoft®internet information S Erver intrinsic objects. The Visual Basic 6.0 COM Session object uses the MySession class of sessionutility assembly to hold the session state, and The Sessionpersistence class of sessionutility to load and save sessions data with SQL Server. The MySession and Sessionpersistence classes are exposed as COM objects using the Regasm.exe utility. The Regasm.exe utility can register and create a type library for the "COM client to consume Framework classes.

The session state information is reloaded during the construction of the object. The constructor (Class_Initialize) 'll the session cookie, session timeout (sessiontimeout), and database Connection string (SESSIONDSN) from the Application object, and create a instance of the class mysession to hold the Sess Ion data. Then the constructor would try to reload the session data from SQL Server with the given cookie. If The SQL Server does not have the session information, or the session has been expired, a new cookie would be issued. If the SQL Sever does return with the session state data, the session state would be stored in the MySession object.

Private Sub Class_Initialize ()
On Error GoTo ErrHandler:
Const method_name as String = "Class_Initialize"
Set mysessionpersistence = New sessionpersistence
Set Myobjectcontext = GetObjectContext ()
Mysessionid = Readsessionid ()
mydsnstring = Getconnectiondsn ()
Mytimeout = Getsessiontimeout ()
Myisnewsession = False
Call Initcontents

Exit Sub
ErrHandler:
Err.Raise Err.Number, Method_name & ":" & Err.Source, Err.Description
End Sub

Private Sub initcontents ()
On Error GoTo ErrHandler:
Const method_name as String = "Initcontents"
If Mysessionid = "" Then
Set mycontentsentity = New mysession
Mysessionid = Mysessionpersistence.generatekey
Myisnewsession = True
Else
Set mycontentsentity =
Mysessionpersistence.loadsession (Mysessionid, mydsnstring, Mytimeout)
End If

Exit Sub
ErrHandler:
Err.Raise Err.Number, Method_name & ":" & Err.Source, Err.Description
End Sub

When the object instance goes out of the scope in the script, the destructor (Class_Terminate) would execute. The destructor would persist the session data using the Sessionpersistence.savesession () method. If This is a new session, the destructor'll also send the new cookie to the browser.

Private Sub Class_Terminate ()
On Error GoTo ErrHandler:
Const method_name as String = "Class_Terminate"
Call Setdataforsessionid
Exit Sub
ErrHandler:
Err.Raise Err.Number, Method_name & ":" & Err.Source, Err.Description
End Sub

Private Sub Setdataforsessionid ()
On Error GoTo ErrHandler:
Const method_name as String = "Setdataforsessionid"
Call Mysessionpersistence.savesession (Mysessionid,
Mydsnstring, mycontentsentity, myisnewsession)

If myisnewsession Then Call Writesessionid (Mysessionid)

Set mycontentsentity = Nothing
Set Myobjectcontext = Nothing
Set mysessionpersistence = Nothing
Exit Sub
ErrHandler:
Err.Raise Err.Number, Method_name & ":" & Err.Source, Err.Description
End Sub
You can download the source code of ASP.net sessionutility project, the COM sessions Manager, and the Demo code by clicking The link at the top of the article.

Demo Program
The demo program are designed to increment and display a number. Regardless of which page is loaded, the number would keep incrementing because the number value is stored in SQL Server and is shared between classic ASP and asp.net.

Steps to Set up the Demo program
Create a new database called Sessiondemodb.
Create the Sessstate table (osql.exe–e–d sessiondemodb–i session.sql).
Create a new virtual directory called Demo.
Turn off ASP session under the ASP Configuration tab.
Copy the web.config, testpage.aspx, Global.asa, testpage.asp, and globalinclude.asp to the virtual directory.
Update the DSN string setting in the Global.asa and web.config. The session timeout setting is optional. The default is minutes.
Install the SessionUtility.dll into the Global Assembly Cache (gacutil/i SessionUtility.dll).
Expose the SessionUtility.dll as a COM object using the Regasm.exe (Regasm.exe sessionutility.dll/tlb:sessionutility.tlb) .
Copy the SessionManager.dll to a local directory and use Regsvr32.exe to register it (regsvr32 SessionManager.dll).
Grant the iusr_<machine_name> account to have read and execute access to the SessionMgr.dll.
Steps to Run the Demo program
Start microsoft®internet Explorer.
Load the testpage.asp for classic ASP. The number "1" should appear in the Web page.
Click Refresh on Internet Explorer to reload the page. The number should be incremented.
Change the URL to testpage.aspx for asp.net. The number should keep incrementing.
The same process can is repeated by starting the TestPage.aspx page A.
Incorporating the COM Object in an Existing ASP application
A common practice in developing ASP applications are to include a file in the beginning all script to share common code S and constants. The best way to incorporate of the custom session object are to add the instantiation code in the common include file. The "Last" is simply to replace all reference to the "Session object" with the custom session variable name.

Limitation/improvement
This solution won't support an existing ASP application which stores a COM object in the Session object. In this case, a custom marshaler are needed to serialize/deserialize of the States in order to use the custom session object. In addition, this solution does is not support storing type arrays of the string. With some additional effort, this feature can is implemented by using the Microsoft®visual Join function to Co Mbine all of the "array elements into" a single string before storing it to session object. The reverse can do using the Visual Basic 6.0 Split function to the split of the string back to individual array elements. On the. NET Framework side, the Join and Split methods are members of the String class.

Conclusion
ASP.net represents a new programming paradigm and architecture, and offers many over advantages ASP. Although porting from ASP to asp.net isn't simple process, the better programming model and improved of ASP . NET'll make the conversion process worthwhile. With the exception of storing a COM object into the session object, the approach described in this article offers a solution That'll make the migration process simpler.

About the Author
Billy Yuen works with Northern California at the Microsoft Technology Center Silicon Valley. This center focuses on the development of Microsoft. NET Framework Solutions. He can is reached at billyy@microsoft.com.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.