Web Garden: In webGarden scenario application pool can have multiple worker process assigned to it. In this case there are possiblilities like users first requests get served by workerprocess A and second request get served by worker process B. assuming you are using In-proc session, in above scenario your worker process A has user session and worker process B was not aware of it so when user tries to request the page and page was serverd by worker process B. Then ultimately user will be asked to reinitiate the session. To overcome this situateion you need to mark the objects of classes who participate into session storing mechanisum as serializable and you need to define the storage space for Session values like Application state server or SQL server for maintaining session state. Why do we need to mark the classes a serializable? Objects while crossing the boundries of application they need to mark as serializable otherwise it's not posible for them to cross the boundries. Application Pool recycling and session values When application pool pro-actively or re-actively recycles the healty/ un-healthy worker process and incase you are using in-process memory for storing the session values, you will loose the session values. To avoid loosing session values use out of process session storing mechanisum. use either SQL server or State server. Web Farm: In web farm scenario multiple machine act like single one using Network load balancing cluster. In this case there are possibilities that first request get serverd by Machine A and second request get server by machine B of NLB cluster. If session get stored in Machine A's in process, how is it possible to have those session value at machine b's in process. To avoid this we need to store session values as shared resource like SQL server or State server. I learned NLB using this article http://www.west-wind.com/presentations/loadbalancing/networkloadbalancingwindows2003.asp To let SQL server participate into session storing mechanisum you need to execute aspnet_regSQL with appropriate switch. -Satalaj
Tags: application pool, webgarden, webfarm, session
Asp.net | session
If you are loosing values stored inside session, below URL will tell you the possible scenarios where this can happen. http://www.revenmerchantservices.com/post/2010/04/19/session-loss-in-webgarden.aspx Many times I had seen developers are using session in asp.net like this. on page load if (session["User"] != null) // do soething else // do some This becomes nightmare to debug or manage session objects as you have created session as per your requirement. Its nightmare to read the code. For best practice follow below steps while working with Session objects. Create a class, name it as XYZSessionManager. where prefix XYZ can be your comapny name or DB name. e.g. Lest create UserInfo entity which will hold FirstName, LastName, properties of User. public class UserInfo { private string _firstName; public string FirstName { get {return _firstName;} set {_firstName = value;} } private string _lastName; public string LastName { get{return _lastName;} set{_lastName = value;} } } Lets add this into session. public class XYZsessionManager { public static UserInfo GetUserInfo { get { if( HttpContext.Current.Session["User"] != null) // Check wheather session object exists or not { return (UserInfo)HttpContext.Current.Session["User"] ; // if exists unbox it into UserInfo object } else { UserInfo u = New UserInfo(); // Instantiate u = XYZController.Provider.GetUserInfo(); // XyzController.provider.GetUserInfo will talke to your Db and it will populate the UserInfo entity HttpContext.Current.Session["User"] = u; // add UserInfo object u into Session. return u; // don't cast the session into UserInfo and return. just return u . Avoid casting (Boxing / unboxing) . } } } } ok we have done with the XYZ session manger. we can do similar steps for other session variable or Cache variables. Now, How to access it into your code? Wrong way : UserInfo u = new UserInfo(); // No need to instantiate u = XyzController.GetUserInfo(); // this will request session Correct way UserInfo u = XyzController.GetUserInfo(); // without creating new instance of an object UserInfo, here we are just refering the refrence to allready created instance. There is no need to create new copy of object who looks like UserInfo and refer it to another new instance. Are you getting my point? Satalaj.
Many times I had seen developers are using session in asp.net like this.
Lest create UserInfo entity which will hold FirstName, LastName, properties of User.
{ get { if( HttpContext.Current.Session["User"] != null) // Check wheather session object exists or not {
return (UserInfo)HttpContext.Current.Session["User"] ; // if exists unbox it into UserInfo object
}
else { UserInfo u = New UserInfo(); // Instantiate u = XYZController.Provider.GetUserInfo(); // XyzController.provider.GetUserInfo will talke to your Db and it will populate the UserInfo entity HttpContext.Current.Session["User"] = u; // add UserInfo object u into Session. return u; // don't cast the session into UserInfo and return. just return u . Avoid casting (Boxing / unboxing) . }
} } }
Tags: session
Tips
Get notified when a new post is published.