[Foreword:] ASP.NET is Microsoft's latest technology-based technology for web-based applications. It provides a large number of benefits than traditional ASP scripting techniques, including: 1) By establishing a better development structure with business logic; 2) instead of using fully compiled code Traditional ASP code translation; 3) It synergies with each support method, which means that the ASP.NET site is higher than the performance of the traditional ASP site. Although there are many potential benefits to convert the existing ASP applications to ASP.NET, some ASP application tasks are important and complicated. Conversion processes may require more resources and bring more risks to applications. One of the ways to solve these problems is to run the ASP and ASP.NET applications while converting a dialog to ASP.NET at a time. To run new procedures at the same time, you need to create a mechanism to share dialog in traditional ASP and ASP.NET. This article discusses how to use the class and serialization characteristics of the .NET framework assembly to share status information. Overview Cookies is the most commonly used method for web applications to identify user dialog, which can be used to identify traditional ASPs and ASP.NET dialog states. In the ASP script, status information is saved in memory and cannot be shared with other applications (such as ASP.NET). If the dialog status is saved in Microsoft SQL Server using the universal format, it can be accessed by traditional ASP and ASP.NET. In this case, a cookie named MySession is used to identify user dialogue. When the user makes a request for a web application, the only cookie generated for the user is used to identify the conversation. In subsequent requests, the browser sends the unique cookie to the server to identify the conversation. A custom object will reload the user dialog data from the SQL Server before being requested by the requested web page. The dialog state in the custom object web page is accessible. After the web request is complete, the conversation data will be saved back to SQL Server if the request is terminated. Figure 1. Data flow simple graph ASP.NET implementation is derived from the System.Web.ui.page class in the ASP.NET. The Page class collects an instance of the HTTPSession object to handle dialog data. In this example, a custom Page class called sessionPage is derived from System.Web.ui.page, providing all features similar to the Page class. The only difference is that the default HttpSession is overloaded with a custom dialog object (using a new modifier for instance variables, C # allows the derived class hidden group members). Public class sessionPage: System.Web.ui.page {... public new mysession session = null; ...} Customized dialogue uses the HybridDictionary object to save the dialog state in memory (HybridDictionary can be used to handle any quantity) Dialogue elements). In order to universal with traditional ASPs, the custom dialogue will limit the dialog data type as a string type (the default HttpSession allows dialogue to save any type of data, universal with traditional ASP).
[Serializable] public class mySession {private HybridDictionary dic = new HybridDictionary (); public mySession () {} public string this [string name] {get {return (string) dic [name.ToLower ()];} set {dic [ Name.tolower ()] = value;}}} Page class is customized to expose different events and methods. In particular, the OnInit method is used to set the initialization state of the PAGE object. If the request does not include a cookie named MySession, a new MySession cookie will be generated for the requester. In addition, the dialog data will be retrieved from SQL Server using custom data access object sessionPersistence from SQL Server. The value of DSN and SessionExpiration is retrieved from Web.config. override protected void OnInit (EventArgs e) {InitializeComponent (); base.OnInit (e);} private void InitializeComponent () {cookie = this.Request.Cookies [sessionPersistence.SessionID]; if (cookie == null) {Session = new mySession (); CreateNewSessionCookie (); IsNewSession = true;} else Session = sessionPersistence.LoadSession (. Server.UrlDecode (cookie.Value) .ToLower () Trim (), dsn, SessionExpiration); this.Unload = new EventHandler (this.PersistSession);} private void CreateNewSessionCookie () {cookie = new HttpCookie (sessionPersistence.SessionID, sessionPersistence.GenerateKey ()); this.Response.Cookies.Add (cookie);} SessionPersistence class uses the Microsoft .NET framework assembly BinaryFormatter comes to serialization and parallelized dialog states in binary formats to provide optimal performance. The binary dialog data for the result is then stored as the image field type. SQL Server.
public mySession LoadSession (string key, string dsn, int SessionExpiration) {SqlConnection conn = new SqlConnection (dsn); SqlCommand LoadCmd = new SqlCommand (); LoadCmd.CommandText = command; LoadCmd.Connection = conn; SqlDataReader reader = null; mySession Session = NULL; try {loadingcmd.parameters.add ("@ ID", new guid (key)); conn.open (); reader = loadingcmd.executeReader (); if (reader.read ()) {datetime lastaccessed =reader .Getdatetime (1) .addminutes; if (Lastaccessed> = DATETIME.NOW) session = deserialize ((byte []) Reader ["Data"]);}} finally {if (Reader! = Null) Reader. Close (); if (conn! = Null) conn.close ();} return session;} private mysession deserialize (byte == null) Return null; MySession session = null; stream stream = null; try {stream = new memorystream (); stream.write (state, 0, state.length); stream.position = 0; iformatter formatter = new binaryformatter (); session = (MySession Formatter.deSerialize (stream);} finally {if (stream! = null) stream.close ();} return session;} At the end of the request, the unload event of the Page class is started, one registered with the unload event Event processing method The serialized dialog data is a binary format and stores the result binary data into SQL Server.
private void PersistSession (Object obj, System.EventArgs arg) {sessionPersistence.SaveSession (Server.UrlDecode (cookie.Value) .ToLower () Trim (), dsn, Session, IsNewSession.);} public void SaveSession (string key, string dsn, mySession Session, bool IsNewSession) {SqlConnection conn = new SqlConnection (dsn); SqlCommand SaveCmd = new SqlCommand (); SaveCmd.Connection = conn; try {if (IsNewSession) SaveCmd.CommandText = InsertStatement; else SaveCmd.CommandText = UpdateStatement Savecmd.Parameters.Add ("@ ID", new guid (key)); savecmd.parameters.add ("@ data", serialize (session)); Savecmd.Parameters.add ("@ Lastaccessed", DateTime.now .Tostring ()); conn.open (); savecmd.executenonQuery ();} finally {if (conn! = Null) conn.close ();}} private byte [] serialize (MySession session) {if (session = = NULL) RETURN NULL; stream stream = null; Byte [] State = null; try {iformatter formatter = new binaryformatter (); stream = new memorystream (); formatter.seri Alize (stream, session); state = new byte [stream.length]; stream.position = 0; stream.read (state, 0, (int) stream.length); stream.close ();} finally {= Stream! = null) stream.close ();} returnity;} sessionPage class, and classes related to it are encapsulated in the SessionutIndility component. In a new ASP.NET project, you need a reference to the SessionUtility component. In order to share dialogue with traditional ASP code, each page is derived from the sessionpage. Once the transplant is completed, the new application can display the basic httpsession using the original HttpSession object to switch back to the Dialog Variable defined in the SessionPage class.
The ASP implements the original ASP dialog only to save the dialog data in memory. To save the dialog data to SQL Server, you need to write a custom Visual Basic 6.0 COM object instead of managing dialog states. This COM object is initialized at the beginning of each web request and reloads the dialog data from SQL Server. When the ASP script is complete, the object will terminate and return the dialog to SQL Server. The main purpose of the Visual Basic 6 COM Session object is to provide access to Microsoft Internet Information Server (IIS) internal objects. Visual Basic 6 COM dialog Objects use the SESSIONUTILITY component to save a dialog, SessionUtility's sessionPersistence class is used to load and save dialog data to SQL Server. Use the Regasm.exe tool to expose the MySession and the SessionPersistence class as a COM object. The Regasm.exe tool can register and create a type library for the COM client to use the framework component class. Status information is reloaded in the constructor of the object. Constructor (class_initialize first retrieving dialog cookies, sessionTimeout, database connection string (SESSIONTSN), and establish an instance of the MySession class to maintain dialog data from the Application object. The constructor then constructed will try to reuse the given cookies into the dialog data from SQL Server. If there is no conversation information in SQL Server, or the conversation has terminated, a new cookie will be generated. If SQL Server returns dialog state data, the dialog status information will be saved 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 If the object instance exceeds the scope of the script, the deconstruction function (class_terminate) will be executed. The deconstruction function will maintain dialog data using the sessionPersistence.sAvestness () method. If it is a new dialog, the deconstruction function sends a 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 routine routine is designed to increase and display a number. Regardless of which page is loaded, the numbers will continue to increase since the numeric values are saved in SQL Server and shared between ASP and ASP.NET. Step 1 of the routine. Create a new database SessionDemodB. 2. Establish a new table sessstate (Osql.exe -e -d sessionDemodb -i session.sql). 3. Establish a new virtual directory Demo. 4. Turn off the ASP dialog in the ASP configuration page. 5. Copy Web.config, TestPage.ASPX, Global.asa, TestPage.ASP, and GlobalInClude.asp to virtual directory. 6. Update the DSN string settings in Global.asa and Web.config. Dialog Timeout Settings is optional, default is 20 minutes. 7. Install SessionUtility.dll to the Global Component Cache (Gacutil / I Sessionutility.dll). 8. Use REGASM.EXE to expose sessionutility.dll as COM object (Regasm.exe sessionutility.dll /tlb:emionutility.tlb). 9. Copy SessionManager.dll to a local directory and register with Regsvr32.exe (Regsvr32 SessionManager.dll). 10. Give the IUSR_