It is easy to develop using ASP.NET, you can have a web application with database operations up in less than half an hour. But this ease of development comes at the expense of several performance hits.

Viewstate
Web sites are stateless by design, which means that anything you have on the page is not retained when you submit the data. What ASP.NET tries to do is to save the state on the page so that state is retained when the page submits its data.

However this quickly becomes unmanageable when you have too many controls on the page, especially if you have large server controls e.g gridviews. As viewstate is stored on the page itself, a simple page with a few form elements may end up sending large amounts of data on postback, the majority of which is the viewstate itself.

Other than increasing page size, there is also the performance cost incurred when serializing and deserializing the viewstate.

Solution:

if a page is an output page and does not do any further processing, you can disable the viewstate for the page <% Page EnableViewState=”false”.

If the control on the page does not handle any events, is not a data-bound control or the data i constantly refreshed on postback, then you can disable the viewstate for the control

If all else fails, you can look into storing viewstate on the session state using SessionPageStatePersister (There is a drawback because the session state may expire before the user is done with what he or she is doing on the page – think blogs, wikis etc)

String concatenation & comparisonHaving experience with int i = 0; i = 1 +2, you decide to go ahead and do the same for strings. string s = “aa” + “bb”This is very bad programing, simply because strings work differently from numeric primitive data types. When you do this type of string concatenation, quite a fair bit of resources are created to hold the strings and results

The same can be said for case insensitive string comparison, usually you would do something like string1.toLower() == string2.toLower(). Same thing occurs, temporary space needs to be created to store the lowered strings and then do a compare.

For loops with easily checked conditions perform far better than for each loops. However if the for loop condition requires a property call (e.g list.count()) then it will perform much slower, in this case get the count before the loop and assign it to a variable and in the for loop check against this variable.

If you have multiple exit conditions, put them when it is first possible to check and also put the one most likely to happen right on top. This reduces the amount of checks needed to exit, which can give you a performance improvement.

This page handles the ‘login’, ‘logout’, ‘clearcookie’ and ‘delauth’ Web Authentication and Delegated Authentication actions. When you create a Windows Live application, you must specify the URL of this handler page.

default.aspx

this is where all the main action takes place

when you first access this page, you will see a sign in link, click on it to begin the sign in process to Windows Live.

Thereafter, Windows Live will redirect you to wlauth-handler.aspx which will then handle the login cookie, and redirect you back to default.aspx

Now you will see something likeNow this application knows that you are the user with ID = “xxxxx”. Please click here to grant consent so that we may access your Windows Live data

In order for the app to access your Live Data, you need to click on the “click here” link

Once that is done, you will see “We have found a consent token associated with your ID.”

Click on Show Me my Contacts for the app to query Windows Live for your contacts and display it out

At first glance, ajax seems to be a cinch with asp.net 3.5, what with all the updatepanel, timers and such. However behind this ease of development hides a danger which most developers may not notice.

Although updatepanels and timers are easy to use, each update triggers a complete postback and if your viewstate is on the page, its an extremely large form post. On the server side, each event of a normal postback is called, and the whole control tree is generated to handle the ajax form post. This is quite intensive both for the server and for the client.

This can be good or bad depending on how you look at it. Its good because it ensures that the postback is a valid one, and also gives you flexibility on the controls you want to change and to include in any javascript that you need, all these being done on the code behind, without the need to fiddle with all the client side javascript required to perform these actions.

On the flip side, there are also client side apis and scriptmanager service references which gives you complete flexibility to go ahead and program using client side javascript.

Nexus initially went with the first approach (i.e a whole bunch of updatepanels), but the results were less than impressive. Moving onto the second approach required a revamp of the whole source code, but this gave the user a much better experience along with substantially reducing server load.

To reduce the footprint on the server side, i utilized ScriptManager’s ServiceReference parameters

ServiceReferences exposes the webservice functions in client side javascript so you can call these webservices without having to write complicated javascript. for example, i have a webservice function called UserFirstLoad(). This function is called the everytime the user logs into Nexus.

Instead of writing javascript to interface to this webservice, ScriptManager automatically writes it for you and exposes it nicely as JSON.UserFirstLoad(), a function which i can easily call and have the capability to do error handling if need be.

As i’m not using UpdatePanel, GUI updates are done directly via DOM. Fortunately Ajax ClientScripts provide some global functions which i can use to

Locate the element i want to update ($get)

Add events to the element ($addHandler)

It is also fortunate that Visual Studio 2008 comes with javascript intellisense, which autocompletes your javascript as you type.

As Javascript is OOP, i decided to build my client script scripts around objects and functions. This gives me the flexibility to overwrite functions and have nice clean code which i can then easily debug with VS2008.