April 30, 2008

VIEWSTATE Serialization

The most important thing for an end user of a website is the how fast the page loads and our responsibility is to make them happy.

Frequent thing to achieve this is to review the code and write the code such a way that it performs better (in terms of processing time) but what after this ?

What if we still can increase the page serving speed. Apart from the code performance it is the ISP's speed and the size of the page that affects the download speed for a web page.


what if we can decrease the size of the page to some extent ( the size generated after the server side scripting finishes ( i mean the final rendered output)

for every server control if it's ViewState is enabled the data is kept in a viewstate in some encoded format(base64 encoded string as everyone says.)

wo when showing a control on a page keeps data in the rendered output of control as well in the viewstate along with the

state ( previous state) information ( doubles the size of final rendered output..!!)

straighaway coming to the topic we can have this VIEWSTATE buddy help us remotely ( not being rendered with the page so by decreasing the final rendered page out put size)

This can be done keeping the VIEWSTATE in the session or keep it at the server. I am keeping it at the web server as I have got the access to the web server and I have developed one web service that deletes files(holding the VIEWSTATE on the web server) after a day. In short its upto you to write code into the SaveViewState and GetViewState methods.

try the following code snippet ( just paste it in the code behind file of your aspx page)

protected override object LoadPageStateFromPersistenceMedium()
{
object _objViewState;
string _strViewState;
_strViewState = GetViewState();
LosFormatter _lFormater =
new LosFormatter();
try
{
//if(_strViewState!=String.Empty)
_objViewState = _lFormater.Deserialize(_strViewState);
//else
//_objViewState="";
}
catch{
throw new HttpException("Invalid ViewState");
}
return _objViewState;
}
protected override void SavePageStateToPersistenceMedium(object viewState)
{
StringBuilder _sBuilder =
new StringBuilder();
StringWriter _sWriter = new StringWriter(_sBuilder);
LosFormatter _lFormater = new LosFormatter();
_lFormater.Serialize(_sWriter,viewState);
_sWriter.Close();
SaveViewState(_sBuilder.ToString());
}
#region Save and Get ViewStateData
private void SaveViewState(string _vState)
{
// keep identicle files user-wise ( byadding sessionid) and page wise (for different pages by aading this.toString())
string _file = @Request.ServerVariables["APPL_PHYSICAL_PATH"]+Session.SessionID+this.ToString()+".txt";
FileStream _fS = new FileStream(_file,System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.ReadWrite,System.IO.FileShare.ReadWrite);
StreamWriter _sW = new StreamWriter(_fS);
_sW.Write(_vState);
_sW.Close();
_fS.Close();
}
private string GetViewState()
{
string _file = @Request.ServerVariables["APPL_PHYSICAL_PATH"]+Session.SessionID+this.ToString()+".txt";
string _retValue=String.Empty;
if(File.Exists(_file))
{
StreamReader _sRead = new StreamReader(_file);
_retValue= _sRead.ReadToEnd();
_sRead.Close();
}
return _retValue;
}

happy programming..........

one more thing not to forget if you are using above code and storing the VIEWSTATE on web server in files.

keep a code in your Session_End in the global.asax.cs file (works when your Session State uses InProc mode) to remove the above files to keep the web-server tension free !!!

Submit this story to DotNetKicks

1 comments:

Anonymous said...

what about session id reuse? session_end isnt foolproof...