Sharing tips for improving the performance of ASP.net Web applications _ Practical Tips

Source: Internet
Author: User

In this article, you will introduce some ways and techniques to improve the performance of ASP.net Web applications. As we all know, solving performance problems is a tedious task, and when performance problems occur, everyone blames developers who write code.

So how is the performance problem to be solved? The following are the points that the. NET developer needs to check before the application system is released.

1.debug="false"

When you create a asp.net Web application, the default setting is "true". It is much more useful to set up as "true" during the development process, but it should be set to "false" when the application publishes the deployment.

<compilation defaultlanguage= "C #" debug= "false" targetframework= "4.0"/> 

2. Close tracing (tracking)

Tracing is very scary and you have not forgotten to close it. If it doesn't work, make sure to edit Web.config and close it. It will consume a large amount of your program resources.

<trace enabled= "false" requestlimit= "ten" pageoutput= "false" tracemode= "SortByTime" localonly= "true" > 

3. Disable session

Be sure to disable this if you are not using session-tracking. You can set the following in each asp.net page:

<%@ page language= "C #" codebehind= "WebForm1.aspx.cs" autoeventwireup= "false" inherits= "Webapplication1.webform1"

enablesessionstate= "false"%> 

4. Deploying applications with release builds

When deploying an application to a production environment, be sure to use the Release version mode, not debug mode. If you use a debug template it is extremely easy to request a timeout. Deploy to a release version, and you will see a great improvement in speed.

5. Close the View state of the page

View state is primarily used after submission, and it is only useful if the data on the page is submitted to this page. The default is "true". If you do not use the form data to return, you can close View state.

<%@ Page enableviewstate= "false"%> 

6. Avoid using Response.Redirect

Redirect (redirection) is cumbersome and is used only to jump from current physical server development to other servers. If only in the development of this server page jump please use Server.Transfer syntax, which will reduce a lot of unnecessary client redirects.

7. Using the StringBuilder class and using the ToString () method

The string class object is immutable, and the method ToString does not significantly improve performance for a string object that is essentially recreating a string object and assigning the new value to the object. When working with strings, it is best to use the StringBuilder class, whose. NET namespace is system.text. Instead of creating a new object, the class operates directly on the string through methods such as Append,remove,insert, and returns the result of the operation through the ToString method. Its definition and operation statements are as follows

int num; 
System.Text.StringBuilder str = new System.Text.StringBuilder (); Creates a string 
str. Append (Num. ToString ()); Add Numeric num 
Response.Write (str. ToString); Show action Results

8. Avoid throwing exceptions

An exception causes a slow speed and causes the application page to display an exception so that no other action can be made. You can use Try/catch to make the exception that appears in the log file.

9. Using the finally method to reclaim resources

If you use a lot of other database connections and access files in your application development, make sure to close them after you run out. Finally blocks are executed at the end of the program, so the code in this area will ensure that the code is executed, and the closure must be done in the development method block.

10. Use client Script validation

Use client-side validation instead of server development end validation. Server development-side data validation will consume a lot of resources on your server development, and will send a lot of page data back.

11. Use of Page.IsPostBack

Make sure that you do not perform too many return codes. Use the Page.IsPostBack property to ensure that only the page initialization logic is executed when a page is first loaded and not back to the responding customer.

12. Using pagination

Most WEB application data is displayed in tabular form. Paging has the advantage of using the application development program efficiently. Each time you try to display a small number of data, this will speed up the page display.

13. Asynchronous invocation using Ajax

Use the Ajax method to make an asynchronous call.

14. Delete Unused httpmodules

For httpmodules, it is understood that a generic HttpApplication event hook can be created that can be inserted into any WEB application. The use of HttpModule is reusable and requires no specific language application code, and only one entry in the web.config is required. In the Web.config file, delete the unused httpmodules.

15. Avoid recursive functions/nested Loops

In any programming language, you need to avoid nested loops and recursive functions to improve performance.

16. Do not use unnecessary Server control

In ASP.net, a large number of server-side controls facilitate program development, but can also result in a loss of performance because each time a user operates a server-side control, a roundtrip process with the server side is generated. Therefore, it is not necessary to use Server control sparingly.

17. When you invoke multiple operations, use multithreading

When the problem arises, the single line Cheng Ka long-running on this issue. Therefore, you can use multiple threads to increase the response speed of your application.

18. Connection and shutdown of the database

Accessing a database resource requires creating a connection, opening a connection, and closing a connection several operations. These processes need to exchange information with the database more than once to authenticate and consume server resources. asp.net provides a connection pool (Connection pool) to improve the performance impact of opening and closing databases. The system places the user's database connection in the connection pool, takes out when needed, recovers the connection when it is closed, and waits for the next connection request. The size of the connection pool is limited, and if the connection pool is maximized to create a connection, it will have a significant impact on performance. Therefore, after the database connection is established, the connection is opened only when the operation is really needed, closed immediately after use, so as to minimize the time to open the database connection, and avoid the occurrence of exceeding the connection limit.

19. Use the SqlDataReader class for fast forward-only data cursors

The SqlDataReader class provides a way to read a data-only stream retrieved from a SQL Server database. If you are creating a asp.net application that allows you to use it, the SqlDataReader class provides a higher performance than the DataSet class. This is because SqlDataReader uses SQL Server's native network data transfer format to read data directly from a database connection. In addition, the SqlDataReader class implements the IEnumerable interface, which also allows you to bind data to a server control. For more information, see the SqlDataReader class. For information about how ASP.net accesses data, see Accessing data through asp.net.

20. High-performance SQL statement rules

    • Try to avoid full table scans
    • Try to avoid determining null values in the WHERE clause for a field
    • Try to avoid using the!= or <> operator in the WHERE clause
    • Try to avoid using or to join conditions in the WHERE clause
    • In and not should also be used with caution
    • Do not perform functions, arithmetic operations, or other expression operations on the left side of the "=" in the WHERE clause
    • UPDATE statement, if you change only 1 or 2 fields, do not update all fields
    • For more than a large amount of data (here hundreds of is even larger) table join, to page and then join, otherwise logical reading will be very high, poor performance
    • Use Varchar/nvarchar as much as possible instead of Char/nchar

21. Caching

Caching is a space in exchange for time technology, popular point that is, you get the data stored in memory for some time, in this short time the server does not read the database, or real data sources, but read your stored in memory data. Caching is an indispensable data processing mechanism for Web site performance optimization, which can effectively relieve database pressure. The caching in ASP.net is mainly divided into:

    • Page Caching
    • Data source Caching
    • Customizing data caching

22. Do load balancing and server addition

Load balancing should not only be viewed as a means of achieving scalability. Although it certainly improves scalability, many times it adds to the performance of WEB applications, as both requests and users emit multiple servers.

23. Do code checking and optimization through FXCOP

FxCop is a code analysis tool that uses a rule-based engine to check out the non-compliant parts of your code; You can also customize your own rules to add to the engine. Some of these rules are:

    • Avoid excessive local variables
    • Avoid using private code that is not invoked
    • Avoid an internal class that is not instantiated
    • Avoid the use of unsealed features
    • Avoid unnecessary casts
    • Initialize a static field of a reference type inline
    • To mark an assembly with NeutralResourcesLanguageAttribute
    • Mark members as Static and so on.

24.asp.net Performance Monitoring Tool

These are the tools used to monitor the performance of your code.

    • . NET Memory Analyzer
    • Red Gate ANTS Performance analysis tool
    • Fiddler
    • Performance counters

These are some of the performance tuning tips. Performance tuning is not a two-day work day, but an iterative process. For Web site developers, when writing asp.net applications, pay attention to performance issues, develop good habits, improve application performance, at least delay the necessary hardware upgrades, reduce the cost of the site.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.