ASP.NET Best Practices v2

22
ASP.NET Best Practices V2 For high Performance Applications Alok Kumar Pandey

description

ASP.NET Best Practices v2

Transcript of ASP.NET Best Practices v2

Page 1: ASP.NET Best Practices v2

ASP.NET Best Practices V2

For high Performance Applications Alok Kumar Pandey

Page 2: ASP.NET Best Practices v2

We discus about1. Practices for Application Development2. Practices for Production Applications

Page 3: ASP.NET Best Practices v2

IntroductionASP.NET is very powerful technology, however it is important to understand how to use that power to build highly efficient, reliable and robust applications. In this discussion we try to highlight the key tips that we can use to maximize the performance of our ASP.NET pages. The above list can be much longer, we will discus the most important ones.

Page 4: ASP.NET Best Practices v2

Plan and research before• Research and investigate how .NET can really benefit you. • .NET offers a variety of solutions on each level of application design and

development.

Page 5: ASP.NET Best Practices v2

String concatenation //using simple string concatenation string strName = string.Empty; for (int i = 0; i < 10; i++) { strName += "Name " + i; }

//Using StringBuilder StringBuilder strBuilder = new StringBuilder(); List<string> objNameList = new List<string>(); objNameList = null;//Set The name list foreach (string strName in objNameList) { strBuilder.Append(strName); }

Page 6: ASP.NET Best Practices v2

Avoid round trips• Implement Ajax UI whenever possible• Use Client Side Scripts. Client site validation• Use Page.ISPostBack

Page 7: ASP.NET Best Practices v2

Save viewstate only when neededViewState is used primarily by Server controls to retain state only on pages that post data back to themselves.

Situation when we do not need ViewState• Our page does not post back.• We do not handle server control events.• We repopulate controls with every page refresh.

Determine the size of your ViewState• By enabling tracing for the page, you can monitor the ViewState size for each

control

Page 8: ASP.NET Best Practices v2

Session variables- Avoid storing too much data in session variables - Make sure your session timeout is reasonable.

To disable session state for a page, - Set the EnableSessionState attribute in the @ Page directive tofalse<%@ Page EnableSessionState="false" %>

To disable session state for a specific application - Use Web.config file of the application.

<sessionState mode='Off'/>

Page 9: ASP.NET Best Practices v2

Server.TransferIf authentication and authorization check is needed duringredirection• Use Response.Redirect

To transfer to pages in other applications • Must use Response.Redirect

Use Server.Transfer to transfer control to pages in the same application• Helpful to show the message like exception etc.

Page 10: ASP.NET Best Practices v2

Server controls when appropriate and avoid creating deeply nested controls• Nothing comes for free• The HTTP protocol is statelessBetter to replace a server control to html control if1. You do not need to retain state across postbacks2. The data that appears in the control is static or control displays

read-only data3. You do not need programmatic access to the control on the server-

side

Deeply nested hierarchies create extra processing like• Repeater, DataList, and DataGrid

Page 11: ASP.NET Best Practices v2

Choose the data viewing control appropriate for your solution• Depend on how we choose to display data in a Web page• Always compare the pros and cons of controls before we use them in

our Web page

Page 12: ASP.NET Best Practices v2

Optimize code and exception handling• To optimize expensive loops, • Use For instead of ForEach in performance-critical code paths. • Also do not rely on exceptions in code • Write code that avoids exceptions• Do not use exceptions to control logic• Check for null values, If it is possible for an object to be null

Page 13: ASP.NET Best Practices v2

DataReader for fast and efficient data binding• Use a DataReader object if we not need to cache data• DataReader is the optimum choice for retrieving read-only data in a

forward-only

Page 14: ASP.NET Best Practices v2

Paging efficiently• Retrieves only the desired data from the database• Reduces back-end work on the database

Page 15: ASP.NET Best Practices v2

Explicitly Dispose or Close all the resources• Cleaned up when an exception occurs, use a try/finally block• Open connection just before need• Close it as soon as done with it• Make sure to call the Dispose or the Close method of the object if

provided

Page 16: ASP.NET Best Practices v2

Disable tracing and debugging• Tracing and debugging are not recommended when our application is

running in production. • We can disable tracing and debugging in the Machine.config and

Web.config

Eg.

<configuration> <system.web> <trace enabled="false" pageOutput="false" /> <compilation debug="false" /> </system.web></configuration>

Page 17: ASP.NET Best Practices v2

Precompile pages and disable AutoEventWireup• Precompiled pages increases the performance,• Slight performance boost by leaving the event wiring to the page

author instead of performing it automatically.

For example, we will need to override Page.OnLoad for the page load event instead of using a Page_Load method

Page 18: ASP.NET Best Practices v2

Stored procedures and indexes• Use compiled stored procedures instead of RAW queries,• Proper indexes,• Reduce the chance of SQL Injection• Substitute the UNION operator by UNION ALL whenever possible.• Substitute Sub-queries by Joins• When in need of obtaining an amount of records form a table, avoid

using the Select Count(*) => table has a column called rows which stores the total amount of records for each table of your database

Page 19: ASP.NET Best Practices v2

Practices for Production Applications1. Generate new encryption keys

Referencehttp://www.codeproject.com/Articles/16645/ASP-NET-machineKey-Generator

2. Encrypt sensitive sections of your web.configThis includes both the connection string and machine key sections. Referencehttp://msdn.microsoft.com/en-us/library/68ze1hb2(VS.80).aspx

3. Use trusted SQL connectionsReferencehttp://idunno.org/articles/276.aspx

Page 20: ASP.NET Best Practices v2

Practices for Production Applications4. Set retail="true" in your machine.config

<configuration><system.web><deployment retail="true"/></system.web></configuration>Referencehttp://msdn.microsoft.com/en-us/library/ms228298(VS.80).aspx

5. Create a new application pool for your site

6. Set the memory limit for your application pool

7. Create and appropriately use an app_Offline.htm file

Page 21: ASP.NET Best Practices v2

Practices for Production Applications8. Develop a repeatable deployment process and automate it

8. configuration files that may be different between the development, staging, or production environments.

9. Build and reference release versions of all assemblies10. Load test

threading and memory issues

Page 22: ASP.NET Best Practices v2

Thank you

?