我刚刚开始研究会话模式,但是花了几个小时后发现了一些我无法通过谷歌搜索找到的疑问..希望我能在这里获得专业建议/解决方案..
我们分别有三台服务器192.xxx.xxx.A,192.xxx.xxx.B,192.xxx.xxx.C(集群)和NLB(硬件)部署我们的应用程序,之前我创建了一个测试网站检查会话状态故障转移,下面是提到的测试网站的代码。
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestCluster.aspx.cs" Inherits="TestCluster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test Cluster</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Test Cluster</h1>
<asp:Button ID="btnSession" runat="server" Text="Session Test"
onclick="btnSession_Click"/>
</div>
</form>
</body>
</html>
在上面的代码中,我只有一个按钮,点击按钮时,我的HTTP请求将会启动。
ASPX.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class TestCluster : System.Web.UI.Page
{
static string seesionTest = "";
static int counterAlive = 0;
static int counterdDead = 0;
static int isPostBackCounter = 0;
static string IspostBack = "";
System.IO.StreamWriter file;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["activeSession"] = "Session is Alive";
isPostBackCounter = isPostBackCounter + 1;
if (isPostBackCounter > 1)
{
IspostBack = "PostBack Happened";
}
}
}
protected void btnSession_Click(object sender, EventArgs e)
{
long lg = 0;
btnSession.Enabled = false;
for (; ; )
{
lg++;
if (lg == 9223372036)
{
btnSession.Enabled = true;
break;
}
httpProcessStart();
}
}
protected void httpProcessStart()
{
file = new System.IO.StreamWriter("e:\\log.txt");
file.WriteLine("httpProcessStart called.");
try
{
file.WriteLine("Session-"+Session["activeSession"].ToString());
if (Session["activeSession"] != null && Session["activeSession"].ToString() != "")
{
file.WriteLine("activeSession");
seesionTest = "";
counterAlive = counterAlive + 1;
string compName = "Response coming from " + System.Environment.GetEnvironmentVariable("COMPUTERNAME");
seesionTest += "<br>" + counterAlive + ":" + Session["activeSession"].ToString() + " ";
string text = "";
if (IspostBack != null && IspostBack != "")
{
text = seesionTest + ", " + " " + IspostBack + ", " + " " + compName;
}
else
{
text = seesionTest + ", " + " " + compName;
}
string lines = text + DateTime.Now + "\r\n";
file.WriteLine(lines);
file.Flush();
file.Close();
}
else
{
file.WriteLine("sessionDead");
btnSession.Enabled = false;
counterdDead = counterdDead + 1;
string seesionDead = "Session is dead restart application";
string text = "";
text = seesionDead;
string lines = text + DateTime.Now + "\r\n";
file.WriteLine(lines);
file.Flush();
file.Close();
counterAlive = 0;
}
}
catch (Exception ex)
{
throw ex;
}
}
}
的web.config
<sessionState mode="StateServer" cookieless="false" timeout="20" stateConnectionString="tcpip=xxx.xxx.xxx.B:42424">
如上所述,我们有三台服务器,所以我使用服务器B来存储会话状态。 提供的代码经过测试并且工作正常,但是这是一个疑问,IP(stateConnectionString =&#34; tcpip =?&#34;)如果建立NLB要么我们使用NLB的虚拟IP还是任何其他IP?如果使用NLB的虚拟IP会话将要存储在哪里?