Home  >  Article  >  Backend Development  >  Methods and techniques to improve the performance of asp.net web applications

Methods and techniques to improve the performance of asp.net web applications

伊谢尔伦
伊谢尔伦Original
2016-12-03 09:23:061097browse

In this article, we will introduce some methods and techniques to improve the performance of ASP.NET web applications. We all know that solving performance problems is a tedious job, and when performance problems occur, everyone blames the developers who wrote the code.

 The following is the translation

Methods and techniques to improve the performance of asp.net web applications

How to solve the performance problem? The following are the points that .NET developers need to check before the application system is released.

 1.debug="false"

 When creating an ASP.NET Web application, the default setting is "true". During development, setting it to "true" is very useful, but when the application is released and deployed, it needs to be set to "false".

<compilation defaultLanguage="C#" debug="false" targetFramework="4.0" />

 2. Turn off tracing

  Tracing is very scary, have you ever forgotten to turn it off? If it doesn't work, make sure to edit web.config and close it. It will take up a lot of your program's resources.

<trace enabled="false" requestLimit=”10” pageoutput=”false” traceMode=”SortByTime” localOnly=”true”>

  3. Disable session

  If you do not use session tracking, please be sure to disable it. 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. Deploy the application using the release version

When deploying the application to the production environment, make sure to use the release version mode instead of the debug mode. It is extremely easy to cause a request timeout if you use debugging templates. Deploy to a release version and you will notice a huge speed improvement.

 5. Close the View State of the page

 View State is mainly used for echoing after submission. It is only useful when the data in the page is submitted to this page. Its default is "true". If you are not using form data postback, you can turn off View State.

<%@ Page EnableViewState="false" %>

6. Avoid using Response.Redirect

Redirect is very troublesome. It is only used to jump from the current physical server to other servers. If you are only redirecting pages within the development of this server, please use the Server.Transfer syntax, which will reduce many unnecessary client redirections.

 7. Use the StringBuilder class and use the ToString() method

 String class objects are immutable. Reassignment of a String object is essentially re-creating a String object and assigning the new value to the object. Its method ToString The performance improvement is not very significant. When working with strings, it's best to use the StringBuilder class, whose .NET namespace is System.Text. This class does not create new objects, but directly operates on strings through methods such as Append, Remove, and Insert, and returns the operation results through the ToString method. Its definition and operation statements are as follows

int num;  
  System.Text.StringBuilder str = new System.Text.StringBuilder(); //创建字符串 
  str.Append(num.ToString()); //添加数值num 
  Response.Write(str.ToString); //显示操作结果

8. Avoid throwing exceptions

Exceptions will slow down the speed and cause the application page to display abnormally, making it impossible to perform other operations. You can use try / catch to log exceptions to the log file.

 9. Use the finally method to recycle resources

 If you use a large number of other database connections and access files during application development, please make sure to close them after use. The finally block is the last one to be executed in the program, so the code inside it will be guaranteed to be executed. The closing code must be executed in this development method block.

 10. Use client-side script verification

 Replace server development-side verification with client-side verification. Server development-side data validation will consume a lot of your server development resources and will require a large amount of page data to be returned.

 11. Use Page.IsPostback

  Please make sure not to execute too many postback codes. Use the Page.IsPostBack property to ensure that only page initialization logic is executed when a page first loads, and no response is posted back to the client.

 12. Use paging

  Most web application data is displayed in tabular form. Pagination takes advantage of application development efficiency. Try to display a small portion of data at a time, which will speed up page display.

 13. Use Ajax to make asynchronous calls

  Use Ajax methods to make asynchronous calls.

 14. Delete unused HttpModules

 For httpModules, we can understand it as: establishing a universal HttpApplication event hook that can be inserted into any web application. Using HttpModule is reusable and requires no application-specific code, just an entry in web.config. In the web.config file, delete unused HttpModules.

 15. Avoid recursive functions/nested loops

  Nested loops and recursive functions need to be avoided in any programming language to improve performance.

 16. Do not use unnecessary Server Control

 In ASP.NET, a large number of server-side controls facilitate program development, but they may also cause performance losses, because every time a user operates a server-side control, a Server-side round trip process. Therefore, Server Control should be used less than necessary.

 17. When calling multiple operations, please use multi-threading

 When the problem occurs, a single thread is stuck running for a long time on this issue. Therefore, you can use multiple threads to increase the responsiveness of your application.

 18. Database connection and closing

 Accessing database resources requires several operations: creating a connection, opening a connection, and closing a connection. These processes require multiple exchanges of information with the database to pass authentication, which consumes server resources. ASP.NET provides a connection pool (Connection Pool) to improve the performance impact of opening and closing the database. The system puts the user's database connection in the connection pool, takes it out when needed, and reclaims the connection when it is closed, waiting for the next connection request. The size of the connection pool is limited. If you are still required to create a connection after the connection pool reaches the maximum limit, performance will be greatly affected. Therefore, after establishing a database connection, only open the connection when operations are actually needed, and close it immediately after use, thereby minimizing the time the database connection is open and avoiding exceeding the connection limit.

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

 The SqlDataReader class provides a way to read a feed-only data stream retrieved from a SQL Server database. The SqlDataReader class provides higher performance than the DataSet class if circumstances arise that allow you to use it when you create an ASP.NET application. This is the case because SqlDataReader reads data directly from the database connection using SQL Server's native network data transfer format. Additionally, the SqlDataReader class implements the IEnumerable interface, which also allows you to bind data to server controls. See the SqlDataReader class for more information. For information about how ASP.NET accesses data, see Accessing Data with ASP.NET.

 20. High-performance SQL statement rules

Try to avoid full table scans

Try to avoid null value judgments on fields in the where clause

Try to avoid using != or operations in the where clause Symbol

Try to avoid using or in the where clause to connect conditions

in and not in. Also use it with caution

Do not perform functions, arithmetic operations or other expression operations on the left side of "=" in the where clause

Update statement, if you only change 1 or 2 fields, do not update all fields

For JOIN of multiple tables with large amounts of data (a few hundred is considered large here), you must first paginate and then JOIN, otherwise the logical reading will be very high and the performance will be very high. Very bad

Use varchar/nvarchar instead of char/nchar as much as possible

 21. Caching

 Caching is a technology that trades space for time. In layman’s terms, it means storing the data you get in memory for a period of time. During this short period of time, the server does not read the database or the real data source, but reads the data you store in memory. Caching is an indispensable data processing mechanism for website performance optimization. It can effectively relieve database pressure. Caching in ASP.NET is mainly divided into:

Page caching

Data source caching

Custom data caching

 22. Load balancing and server addition

Load balancing should not only be regarded as achieving scalability a means of sex. While it certainly improves scalability, many times it increases the performance of web applications because requests and users are spread across multiple servers.

 23. Code inspection and optimization through FxCop

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

Avoid too many local variables

Avoid uncalled private code

Avoid uninstantiated inner classes

Avoid unsealed features

Avoid unnecessary casts

Initialize static fields of reference types inline

Mark the assembly with NeutralResourcesLanguageAttribute

Mark members as Static and so on.

 24.ASP.NET Performance Monitoring Tools

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

.NET Memory Analyzer

Red Gate ANTS Performance Analysis Tool

Fiddler

Performance Counter

 Conclusion: The above are some tips for performance tuning. Performance tuning is not a day or two work, but an iterative process. For website developers, paying attention to performance issues when writing ASP.NET applications, developing good habits, and improving application performance can at least postpone necessary hardware upgrades and reduce the cost of the website.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn