负载均衡下设置Session需要注意的一点,防止Session丢失
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.