How to Share Session State Between Classic ASP and

来源:互联网 发布:C语言roll程序 编辑:程序博客网 时间:2024/05/16 09:02
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
ASP Implementation
The native ASP Session can only store Session data in memory. In order to store the Session data to SQL Server, a custom Microsoft⮠Visual Basic⮠6.0 COM object is written to manage the Session State instead of using the native Session object. This COM object will be instantiated in the beginning of each Web request and reload the Session data from SQL Server. When the ASP script is finished, this object will be terminated and the Session State will be persisted back to SQL Server.

The primary purpose of the Visual Basic 6 COM Session object is to provide access to the Microsoft⮠Internet Information Server 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 Session 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) will first retrieve the Session cookie, Session timeout (SessionTimeOut), and database connection string (SessionDSN) from the Application object, and create an instance of the class mySession to hold the Session data. Then the constructor will 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 will be issued. If the SQL Sever does return with the Session State data, the Session State will 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 scope in the script, the destructor (class_terminate) will execute. The destructor will persist the Session data using the SessionPersistence.SaveSession() method. If this is a new Session, the destructor will also send the new cookie back 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 Session Manager, and the Demo code by clicking the link at the top of the article.

Demo Program
The demo program is designed to increment and display a number. Regardless of which page is loaded, the number will 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 20 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 be repeated by starting the testPage.ASPx page first.
Incorporating the COM Object in an Existing ASP Application
A common practice in developing ASP applications is to include a file in the beginning of each script to Share common codes and constants. The best way to incorporate the custom Session object is to add the instantiation code in the common include file. The last step is simply to replace all reference to the Session object with the custom Session variable name.

Limitation/Improvement
This solution will not support an existing ASP application that stores a COM object in the Session object. In this case, a custom marshaler is needed to serialize/deserialize the States in order to use the custom Session object. In addition, this solution does not support storing type arrays of the string. With some additional effort, this feature can be implemented by using the Microsoft⮠Visual Basic⮠6.0 Join function to combine all of the array elements into a single string before storing it into Session object. The reverse can be done using the Visual Basic 6.0 Split function to split 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 advantages over Classic ASP. Although porting from ASP to ASP.NET is not a simple process, the better programming model and improved performance of ASP.NET will make the conversion process worthwhile. With the exception of storing a COM object in the Session object, the approach described in this article offers a solution that will make the migration process simpler.

About the Author
Billy Yuen works in Northern California at the Microsoft Technology Center Silicon Valley. This center focuses on the development of Microsoft .NET Framework solutions. He can be reached at billyy@microsoft
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击