Microsoft KB Archive/323262

= How to use ASP.NET session state SQL Server Mode in a failover cluster =

Article ID: 323262

Article Last Modified on 11/16/2004

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0

-



This article was previously published under Q323262





SUMMARY
This article describes the information that you must have to configure a Web server cluster to use the SQL session state that runs in a failover cluster. If you anticipate many requests on your Microsoft ASP.NET applications, you may install a Web farm of Web servers that are running on Windows Network Load Balancing (WNLB).

By using WNLB, you can reveal a virtual access point to your users. WNLB shares the workload across all members of the Web farm transparently. If your Web application uses a session, you can store the ASP.NET session state in SQL Server to gain more reliability and to improve virtual access for the users.



MORE INFORMATION
ASP.NET offers significant versatility in saving a session state. This solves most problems that may occur while you run earlier ASP applications in a Web farm. You can configure the Web.config file of a particular application to use the SQL session state on a failover cluster.

Microsoft SQL Server offers you centralized storage of a session state in a Web farm. It also offers the transactional capabilities that provide reliability to most relational database systems. You can use SQL Server to save a session.

Configure ASP.NET to store session state in SQL Server
When you set the value of the mode attribute to SqlServer in the sessionState element of the Web.config file, an ASP.NET worker process stores the objects that belong to the client session collection in SQL Server at the end of each Web request.

For additional information about how to install SQL session state in a SQL Server cluster, click the following article number to view the article in the Microsoft Knowledge Base:

311209 How to configure ASP.NET for persistent SQL Server session state management

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

317604 How to configure SQL Server to store ASP.NET session state

To store the ASP.NET session state in SQL Server, change the  element in the Web.config file as follows:    Note Verify that you use the correct case when you specify the  element and the associated attribute values. This code is case sensitive.

Sample application for testing
 Use Visual C# .NET to create a new ASP.NET Web Application named ASPNETSession.  In HTML view of WebForm1.aspx, replace the existing code with the following code: <%@ Page language=&quot;c#&quot; AutoEventWireup=&quot;false&quot; %> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; >   WebForm1  <body MS_POSITIONING=&quot;GridLayout&quot;> <form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;> <asp:TextBox id=&quot;TextBox1&quot; style=&quot;Z-INDEX: 101; LEFT: 23px; POSITION: absolute; TOP: 38px&quot; runat=&quot;server&quot;></asp:TextBox> <asp:Button OnClick=&quot;Button1_Click&quot; id=&quot;Button1&quot; style=&quot;Z-INDEX: 104; LEFT: 190px; POSITION: absolute; TOP: 37px&quot; runat=&quot;server&quot; Text=&quot;Set Session Value&quot;></asp:Button> <asp:Label id=&quot;Label1&quot; style=&quot;Z-INDEX: 103; LEFT: 24px; POSITION: absolute; TOP: 75px&quot; runat=&quot;server&quot; Width=&quot;153px&quot; Height=&quot;21px&quot; BorderColor=&quot;#C04000&quot; BorderStyle=&quot;Solid&quot; BorderWidth=&quot;1px&quot;></asp:Label> <asp:Button OnClick=&quot;Button2_Click&quot; id=&quot;Button2&quot; style=&quot;Z-INDEX: 102; LEFT: 190px; POSITION: absolute; TOP: 75px&quot; runat=&quot;server&quot; Text=&quot;Get Session Value&quot;></asp:Button>

</HTML> private void Button1_Click(object sender, System.EventArgs e)       { Session[&quot;myval&quot;]=TextBox1.Text;

}

private void Button2_Click(object sender, System.EventArgs e)       { Label1.Text=Session[&quot;myval&quot;].ToString;

} </li> Compile and then run the application.

Note Make sure that the Web farm of Web servers is set up, and that SQL Server Services are started before you run the Web application.</li> Type any string in the first text box, and then click Set Session Value.</li> The session state is stored in SQL Server with a unique session ID. Examine the session state that is stored in the ASPStateTempSessions table of the ASPState database.</li> Stop the first Web Server that your request is processed from.</li> In the browser, click Get Session Value.</li> Note that the string that is stored in the session by the first Web server appears in the text box although the first Web server is stopped. The second Web server runs the service for the request.</li></ol>

Note Because of a bug in the System.data.dll file in the Microsoft .NET Framework 1.0 and the .NET Framework 1.1, SqlConnections that were connected to the old (now stopped) SQL Server-based server will still be kept in the connection pool. Therefore, you may see some errors in the request because ASP.NET is still using those dead connections in a browser. But when all these pooled dead connections are used and discarded, it will start working again.

<div class="references_section">