首页 > 编程 > ASP > 正文

How to Share Session State Between Classic ASP and

2024-05-04 11:06:31
字体:
来源:转载
供稿:网友
中国最大的web开发资源网站及技术社区,
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 [email protected]
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表