Last post Dec 15, 2011 09:22 AM by BoxheadMonkey
Dec 13, 2011 09:34 AM|BoxheadMonkey|LINK
I have the following code on one page on my system:
Dictionary<int, string> objectsDict = new Dictionary<int, string>();
foreach (GridViewRow rowItem in gvContacts.Rows)
chk = (CheckBox)(rowItem.Cells.FindControl("cbSelectContact"));
contactDetails = ((Label)(rowItem.Cells.FindControl("lblName"))).Text + "~" + gvContacts.DataKeys[rowItem.RowIndex].Values["Emails"].ToString();
The code works fine but if the page jumps from one of the loadbalanced servers to the other, the session is lost (or an old session from the new server is used). I was under the impression that Dictionary was serializable and therefore woould maintain sessions
across loadbalanced servers?
cheers for any advice.
Dec 13, 2011 04:51 PM|sivilian|LINK
Can you explain what you expect to happen once you redirect? Do you expect the Dictionary object (objectDict) to be populated?
Unless I missed something, the code snippet you provide loads a dictionary with objects, but nothing is done with it. Once the page redirects, it is lost. This has nothing to do with a load balanced architecture.
Dec 14, 2011 04:55 AM|BoxheadMonkey|LINK
oops! Missed a line during paste!:
HttpContext.Current.Session["ObjectsList"] = objectsDict;
The session is then used on the next page. If the redirect changes server, then the session either doesn't exist or uses the last session create on that server.
addition: i have now posted this question as the problem seems to be with all my session variables not just the dict:
Dec 14, 2011 07:02 AM|sivilian|LINK
Refer to this article to set up Session State in a load balanced environment.
ASP.NET load balancing and ASP.NET state server
hope this helps,
Dec 14, 2011 07:42 AM|BoxheadMonkey|LINK
Thanks - there is just one thing that confuses me: if I have two servers being load balanced, do i just set one of them as the state server? If so, what happens if this server goes down - does the other server continue to work correctly or will it no longer
save session information?
Dec 14, 2011 09:00 AM|sivilian|LINK
Unfortunately yes. If you set it this way, and the server which is set as the state server goes down, you will lose session information.
One way to alleviate this however is to use SQL Server session state. This will save the session to SQL Server.
All you have to do in order to get this to work is to modify your web.config:.
sqlConnectionString="data source=127.0.0.1;user id=<username>;password=<strongpassword>"
Dec 14, 2011 09:40 AM|BoxheadMonkey|LINK
great - at least I know! I can't use SQL as i need to keep db traffic to a minimum!
If i needed to take a server off line for a while, whould i just need to change the session mode on the remianing server to InProc in the web.config for it to start to maintain it's own sessions?
Thanks for your assistance
Dec 14, 2011 10:02 AM|sivilian|LINK
Yes. Changing it to InProc is a good idea. You probably have to recycle the app pool, or do an IISReset before it starts to work.
Dec 15, 2011 09:22 AM|BoxheadMonkey|LINK
Thanks again for all your help :)