Tuesday, November 22, 2011

ASP.Net State management

What is State Management?
State management is the process by which you maintain state and page information over multiple requests for the same or different pages.
ASP.NET includes several options that help you preserve data on both a per-page basis and an application-wide basis.

Types of State Management
There are two basic types of State Management:
1.       Client-Based State Management
2.       Server-Based State Management
Client-Based State Management:
Client based state management techniques stores data on the client in various ways. Client based state management techniques are:
A.      View state
B.      Control state
C.      Hidden fields
D.      Cookies
E.       Query strings
View state:
The view state represents the state of the page when it was last processed on the server. It's used to build a call context and retain values across two successive requests for the same page. By default, the state is persisted on the client using a hidden field added to the page and is restored on the server before the page request is processed.
If the amount of data stored in the ViewState() property exceeds the specified value in the MaxPageStateFieldLength() property, multiple hidden fields are used to store View state data.
                Advantages:     
a)      Server resources not required.
b)      Simple implementation
c)       automatic retention of page and control state
d)      Enhanced security features. The values in view state are hashed, compressed, and encoded for Unicode implementations.
Disadvantages:
a)      Scope is limited to only single page.
b)      Performance. The view state is stored in the page itself, so increase the page size.
c)       Security. The view state is stored in a hidden field on the page. Although view state stores data in a hashed format, it can be tampered with. 
Control state:
If you create a custom control that requires view state to work properly, you should use control state to ensure your control work properly though developers disable view state. The ControlState() property allows you to persist property information that is specific to a control and cannot be turned off like the ViewState() property.
     Advantages:     
a)      Server resources not required.
b)      Reliable. Because control state can not be turned off like ViewState.

Disadvantages:
a)      Some programming is required.
Hidden Fields:
A hidden field acts as a repository for any page-specific information that you want to store directly in the page. ASP.NET allows you to store information in a HiddenField control, which renders as a standard HTML hidden field. A hidden field does not render visibly in the browser.
You must submit the page using an HTTP POST command in order for hidden-field values to be available.

                Advantages:     
a)      Server resources not required.
b)      Simple implementation
c)       Widespread support.
Disadvantages:
a)      Potential security risk.
b)      Performance. The hidden fields are stored in the page itself, so increase the page size.
c)       Does not support rich data types to store.
Cookies:
A cookie is a small amount of data that is stored either in a text file on the client file system or in-memory in the client browser session. Browser sends with cookies values with every page request to the same server. Cookies can be temporary or persistent.

Advantages:     
a)      Server resources not required.
b)      Simple implementation
c)       Configurable expiration rules
d)      Data persistent.
Disadvantages:
e)      Size limitation: Most browsers support maximum 4096 bytes cookies.
f)       User configuration refusal: User can disable cookies in their browser.
g)      Security risk: Can be tempered.  
Query strings:
A query string is information that is appended to the end of a page URL. Query strings provide a simple but limited way to maintain state information.  Some browsers and client devices impose a 2083 character limit on the length of the URL.
You must submit the page using an HTTP POST command in order for query string values to be available.
Advantages:     
h)      Server resources not required.
i)        Simple implementation
j)        Widespread support.
Disadvantages:
k)      Size limitation: Some browser limits 2083 chars on the length of URLs.
l)        Security risk: Information is visible to the user.  
Server-Based State Management:
Server based state management techniques store data in memory on the server.  Server based state management techniques are:
A.      Application state
B.      Session state
C.      Profile Properties
Application state:
Application state provides a method of storing data global to whole application. These data are visible to entire application and shared by all active sessions. That’s why Application state variables are global variables for an ASP.Net application.
Advantages:     
m)    Simple implementation
n)      Application wide scope
Disadvantages:
o)      Application scope in case of Web Garden or Web Firm.
p)      Limited Durability of data.
q)      Requires server memory.
Session state:
Session state provides a method of storing session specific information that is visible only within the session.
Advantages:     
r)       Simple implementation
s)       Session specific events
t)       Data can persists across multiple process
Disadvantages:
u)      Application scope
v)      Limited Durability of data.
w)    Requires server memory

Wednesday, November 16, 2011

How can I improve performance and speed of my asp.net web application?

This is a most frequently asked question in asp.net communities. I will list out some points that are important to speed up asp.net web application.
  1. Always use Page.IsValid property. Page.IsValid confirms that all validation controls in page returns valid. Execute server side code only when page is valdated.
  2. Use Page.IsPostBack property to minimize redundant processing. Confirm that only data is loaded on first time page load.
  3. Use HttpServerUtility.Transfer method.
  4. Use StringBuilder for large string manipulation. Strings are immutable. The large is string operation you need the more benefit you can get from StringBuilder.
  5. Avoid unnecessary type conversions. Type casting is costly so do it only when necessary.
  6. Use ViewState only when necessary. Use of ViewState increase page size which increase traffic load on network.
  7. Restrict use of Session state. Use of Session and Application state directly have affect on server memory. 
  8. Remove unnecessary using’s.
  9. Build your application on Release mode. Release mode avoid including debug information which increase execution of application.
  10. Precompile your Application.
  11. Use Asp.Net Caching.
  12. Enable buffering. 
  13. Use transaction properly. 
  14. Limit use of Asp.Net Server controls.
  15. Use client side validation.
  16. Don’t rely on Exception.
  17. Use finally block to dispose unnecessary objects.
  18. Avoid unnecessary round trip to the server.
  19. Avoid Use of ViewState encryption unless necessary.
  20. Disable Debug mode.
  21. Rewrite call incentive COM components in managed code.
  22. Avoid blocking and long running tasks.
  23. Take advantage of HttpResponse.IsClientConnect before performing large operation.
  24. Optimize extensive loops. for loop is faster than foreach loop. Use for loop where possible.
  25. Avoid using deep hierarchy of controls.
  26. Avoid using Page.DataBind.
  27. Minimize calling DataBinder.Eval.
  28. Use Repeater when you need to display read only data.
  29. Consider using ListView instead of GridView.
  30. Use Response.Write for formatting output.
  31. Use paging properly for large result sets. ListView or GridView that may contain more than 20 rows should have paging allowed.
  32. Use SSL only for pages that require it.
  33. Disable tracing and debugging.
  34. Consider using HTTP compression.
  35. Reduce No of HTTP requests.
  36. Removed unnecessary white space from page.
  37. Avoid MS Ajax, Consider using light weight java script library like JQuery, ExtJs etc.
  38. Use index on all foreign keys.
  39. Minimize use of cursor.
  40. Minify JS files while deploying.
  41. Put CSS on top of page.
  42. Put JS on bottom of page.
  43. Make css and java script files external.
  44. Use Content Deliver Network for static content.
  45. Consider CSS and Image sprite to reduce number of request. 
  46. Do not scale image in HTML.
  47. Batch SQL Statement to reduce round trip.
  48. Use SETNOCOUNT ON in stored procedure.
  49. Use SET TRANSACTION ISOLATION LEVEL READ COMMITTED below SETNOCOUNT ON and before SELECT queries to ensure no locks during reads.
  50. Consider index on columns used in Where, Order by, Group by and Distinct clause.
  51. Use Stored Procedure for Data Access.
  52. Use DataReader class for fast forward only data cursor.
  53. Rewrite inefficient queries to improve performance.
  54. Write paged queries to ensure that you retrieve only required data from database.
  55. Consider using connection pooling.

Thanks