负载均衡下设置Session需要注意的一点,防止Session丢失

本文介绍在使用ASP.NET Web应用程序时,在网络负载均衡环境下如何避免由于不同Web服务器间的Session状态丢失问题。通过设置相同的网站实例ID,并提供具体的VBScript脚本实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Preserving Session State with ASP.NET Web Applications in Network Load Balancing

Updated: August 22, 2005

If you use SqlServer or StateServer session state mode, session state might be lost when you run an ASP.NET Web application in a Network Load Balancing server cluster. To maintain session state across different Web servers in the cluster, the application path of the Web site (for example, \LM\W3SVC\2) in the IIS metabase must be the same for all of the Web servers in the cluster.

On one Web server, the instance ID of the Web site where the ASP.NET application is hosted might be 1 (where the application path is \LM\W3SVC\1). On another Web server, the instance ID of the Web site might be 5 (where the application path is \LM\W3SVC\5).

 

负载均衡下需要把不同机器上站点的instance ID都设为同一个。否则session会丢失,即使你把session设为了保存在sqlserver。

需要运行如下脚本

MySitesIdList.vbs

Function ProcessWebSite(ServiceType, SiteNumber)
Set IISWebSite = getObject("IIS://localhost/" & ServiceType & "/" & SiteNumber)
Set IISWebSiteRoot = getObject("IIS://localhost/" & ServiceType & "/" & SiteNumber & "/root")
ProcessWebSite = IISWebSite.ServerComment
Set IISWebSiteRoot = nothing
Set IISWebSite = Nothing
end function

Function ShowSites(ServiceType, ClassName, Title)
Wscript.echo "Web Sites Description"
Wscript.echo "==============================================================="
Set IISOBJ = getObject("IIS://localhost/" & ServiceType)
for each Web in IISOBJ
if (Web.Class = ClassName) then
wscript.echo Ucase(ServiceType) & "/" & Web.Name & _
Space(17-(len(Ucase(ServiceType))+1+len(Web.Name))) & " " & _
ProcessWebSite(ServiceType, Web.name)
end if
next
Set IISOBj=Nothing
WScript.Echo ""
End function

Call ShowSites("w3svc", "IIsWebServer", "Web")

把脚本考到机器上然后运行cscript  MySitesIdList.vbs

 

Dim WebService
Dim oldstr
Dim newstr
Dim args
Set args = WScript.Arguments
If args.Count < 1 Then
    Wscript.Echo "Must have original instance id and new instance id" &     chr(10) & chr(13) & _
    "usage:  moveinstance.vbs 1 5"  & chr(10) & chr(13) & _
"Moves instance 1 to instance 5"
    WScript.Quit()
End If
Set WebService = GetObject("IIS://LocalHost/W3SVC")
oldstr = args(0) 'old instance
newstr = args(1) 'new instance
WebService.MoveHere oldstr,newstr
WebService.SetInfo
Set WebService = nothing
Set args=nothing
WScript.echo "DONE"

运行

cscript ChangeSiteId.vbs 1 5

and then press ENTER. Instance ID就从1改到了5. 

 

转载于:https://www.cnblogs.com/beyondjay/archive/2009/03/26/1422027.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值