Here is my situation: I will have a 开发者_如何学Goweb page with TreeView control, which is supposed to be expadable/collapsable/editable. All good so far. I know that postback is used heavily in this control which brings me to another problem that's specific to our production set-up.
We have 2 web servers here and a load balancer, the load balancer doesn't work properly and unfortunately can't guarantee that the page is going to get submitted to the same web server. In other words, a situation may happen when session will not remember who you are or any variables (such as treeview viewstate) saved in session will get lost.
I need to make sure that my treeview always posts back to the same server. My solution was when the page first loaded get the web server's IP adress and rewrite all the links/buttons to point to that IP specifically to bypass the load balancer. The problem arises with the treeview. How do I make sure that it posts back always to the same IP address?
Thanks
Viewstate is stored in the form, not in a session variable. If you examine the html source for your page, you should see a "_viewstate" hidden form field.
You might try running this on your local machine using Cassini to help isolate the problem.
Also, this is yet another example of why session variables should be avoided. Anything stored in a session can also be stored in a database, which is where it should live.
I think the answer here is to remedy the load balancer situation, not hack around it.
First off, just to be clear, Viewstate is entirely form data posted back to the server, so that is not inherently a problem like Session data is. That is, if your servers are set up correctly you can successfully do cross-server postbacks and retain Viewstate. See Jeff Atwood's blog entry about sharing machine keys or disabling Viewstate keying.
For in-proc Session, of course you can't, so that may still be a deal breaker. What you should really do is either:
- Configure the load balancer to support session affinity so all requests go to the same server
- Use SQL session or a different state server shared between the web servers
精彩评论