Efficient ASP.NET

zhaozj2021-02-16  115

0 Preface

ASP.NET is a new generation of dynamic server pages launched by Microsoft. The current version is 1.0. It is an important part of Microsoft's .NET framework structure. In terms of performance, ASP.NET is more powerful and stable than previous ASP 3.0: ASP.NET provides less easy to write, more clear code, which is easy to reuse and sharing; ASP.NET uses after compiling Language, thereby improving performance and scalability; ASP.NET uses web forms to make development more intuitive, using object-oriented technology to promote the reuse of components. In addition, ASP.NET also includes page events, web controls, buffer technology, and server controls and improvements to data bundle. In short, ASP. NET powerful features make it a weapon for developing a web application.

1 ASP.NET features

ASP.NET page has many unique features:

First, the ASP.NET page is structured. That is, each page is a class that effectively inherits the .NET WebPage class, some methods that can be rewritten in the survival of the WebPage class. Since the functionality of a page can be broken down into an event handler with a clear meaning, the ASP.NET page is easier to understand.

Second, the control can encapsulate reusable functionality, users do not have to write code, you can implement status management, confirmation, data processing, etc., thereby simplifying many common programming tasks.

Third, the ASP.NET page can be created in the VS.NET, which can also create transaction logic and data access components used in the ASP.NET page, and can debug the ASP.NET page in the editor.

Fourth, the biggest advantage of the ASP.NET page is that it does not have an ASP dependence on non-structural script language (including VBScript and JScript). ASP.NET allows users to write code using any language supported by ASP.NET, in addition to supporting VB, C #, etc., any programming that can be compiled into MSIL, can become a program language for making an ASP.NET page, and Cross-language operations can be implemented.

It can be seen that ASP.NET provides a real intermediate language execution framework for web applications.

2 Metrics that affect the performance of ASP.NET applications

Here is four common performance metrics:

. execution time

Treating a request for a request, typically calculate the time calculated between the first byte returned to the client to the client. The execution time directly affects the calculation of throughput.

. Response time

Returns the length of time between the first byte from the issuance request to the server. For client users, this is usually the most intuitive aspect in performance. If the application response time is long, the user may feel impatient and go to another site. The response time of the application is not related to the rate of throughput (even inverse ratio).

Scalability

The ability to measure the application in obtaining more resources (memory, processors, or computers). It is often calculated according to the rate of throughput relative to the number of processors.

Swallowing

The number of requests that the web application can handle within the unit time, often measure the number per second. The throughput can be different depending on the load (number of client threads) applied to the server. This is often considered to be the most important performance metric to be optimized.

In order to write a normal and efficient application, it is necessary to maintain the reasonable value range of these metrics.

3 Improve the performance of ASP.NET applications

As can be seen from the above, the ASP.NET model provides many built-in performance enhancements, especially two features in HTTP requests: First, when requesting the ASP.NET page for the first time, dynamically compile the Page class. Example. Public Language Runtime (CLR) compiles the ASP.NET managed page code real-time (JIT) as the local code of the processing server. Second, after compiling the Page class instance for the first request, it is cached on the server. An instance of such a cache will be executed for each request to which the page is subsequent. Unless the initial source of the page or one of its dependencies change, the compilation of the Page class only results in an access speed of the ASP.NET page after the initial request. Although this, the application to ensure that the user written can handle multiple HTTP requests at the same time, thereby improving the performance of the ASP.NET application, but also starts from the following aspects to ensure that the code written can achieve acceptable performance. level:

0) To disable debug mode. Always remember to disable debug mode before deploying production applications or performs any performance measurements. If debug mode is enabled, the performance of the application may be very affected. Usually in the web.config file, the application is set to debug mode: the method is set to "false" in the application design, of course, can set it to "True" in the application design phase, which is easy to debug.

1) Disable it when you do not use a session state. Not all applications or pages need to be targeted about the specific user's session status, at which time any application or page that does not require a session state is disabled. The method is to set the EnableState attribute in the @ Page instruction to false. For example, <% @ page enablessionState = "false"%>. Note If the page needs to access the session variable, it will not be created or modified in the future, set the EnableSsesTate property in the @ page instruction to readonly. To disable the application's session state, you need to set the MODE property to OFF in the sessionState configuration section of the application web.config file. E.g,

.

2) Try to use the StringBuilder class when string. The string is not variable in the .NET framework, which means changing the operator and method of the string return to the change of the string, which means that performance has improved space. When performing a large string operation, use the StringBuilder class is a better choice. Tested, the speed of the Append method using the StringBuilder class is much faster than using a string connection. .

3) minimize form futures. You can use Page.isPostBack to avoid unnecessary processing on the round-trip. If you write the code for processing the server control return process, you may have to execute the web form code when the page is first requested, not the code that the user is executed when the user is included in the HTML form included. Whether or not this page is generated by the response server control event, use the ISPostBack property with conditional execution code. For example, the following code demonstrates how to create a database connection and command, which is bound to the DataGrid server control when requested the page for the first time.

Void Page_Load (Object Sender, Eventargs E)

{

// ... set up a connection and command here ....

IF (! page.ispostback) {

String query = "Select * from authors where firstname Like '% Justin%'"; MyCommand.fill (DS, "Authors");

MyDataGrid.databind ();

}

}

Since each request is executed, the code checks if the iSPostBack property is set to false. If set to false, execute the code; otherwise, do not perform the code, so that the transmission speed can be accelerated, thereby improving the performance of the ASP.NET application.

4) Use server controls in an appropriate environment. Check the application code to ensure that the use of the ASP.NET server control is necessary. Although they are very easy to use, server controls don't always complete the best choice for tasks because they use server resources. In many cases, a simple rendering or data binding replacement can complete the task. However, if you want to operate server control attributes, process server control events or save your server control or save your view status, use server controls.

5) Save the server control view status only when necessary. Auto View Status Management is the functionality of the server control, which allows the server control to re-filled their attribute values ​​on the round-trip, without having to write any code. However, because the view status of the server control is to and from the server in the hidden form field, this feature does have an impact on performance. For example, if you bind a server control to data on each round-trip, you will replace the saved view status with the new value obtained from the data binding operation. In this case, the disable view status can save time. By default, view status is enabled for all server controls. To disable the view status, set the control's EnableViewState property to false, as shown in the following DataGrid server control sample.

You can also use the @ page instruction to disable the view status of the entire page. This will be very useful when you don't return from the page to the server.

<% @ Page EnableViewState = "false"%>

6) Try not to rely on the exception in your code. Because exception greatly reduces the performance of the application, do not use them as a control program process. If an exception may occur in the code, you can refer to the following method: check the null value or check the specific value before applying the mathematical operation. The following program clip demonstrates the code that may lead to an abnormal code and improved processing. The results produced are identical.

// consider changing this ...

Try {

Result = 100 / NUM;

}

Catch (Exception E) {

Result = 0;

}

// To this ...

IF (Num! = 0)

Result = 100 / NUM;

Else

Result = 0;

7) Try to use early bindings in VB.NET or JScript code. In the past, developers like to use VB, VBScript, and JScript reasons for their "non-type" nature. Simply use them to create variables, they don't need explicit type declarations. When allocated from a type to another, the conversion will be executed automatically. However, this convenience may greatly reduce the performance of the application. The VB language is now supported by using the Option Strict Compiler instruction to support type security programming. For the purpose of backward compatibility, the ASP.NET does not enable this option by default. However, in order to get the best performance, it is highly recommended to enable this option on the page. To enable Option Strict, include the strict property in the @ Page instruction; or for the user control, include this property in the @ control instruction. The following program code demonstrates how to set this property. <% @ Page language = "vb" strake = "true"%>

<%

DIM B

DIM C As String

'This Will Cause A Compiler Error.

A = "hello"

'This Will Cause A Compiler Error.

B = "world"

'This Will Not Cause A Compiler Error.

C = "!!!!!!"

'But this will ...

C = 0

%>

8) Migrate the intensive COM component to the managed code. The .NET framework provides a very simple way to interact with traditional COM components. The advantage is that the new platform can be utilized while retaining existing investments. However, in some cases, the performance cost of retaining the old components is exceeded to migrate the component to the managed code. Usually the performance impact of COM interoperability is in proportion to the number of functions that are modulated by the function of the function, or from the unmanaged code to the data encoded by the managed code. Therefore, any COM component migration to the hosted code will be required to be adjusted to the hosted code. Alternatively, you can consider redesigning components (with less calls) or more data in a single call.

9) Use the SQL Server stored procedure for data access. In all data access methods provided in the .NET framework, SQL Server-based data access is a recommended selection for generating high performance and scales web applications. Additional performance improvements can be obtained by using the compiled stored procedure instead of a special query by using the hosting SQL Server provider.

4 Conclusion

The production and development of ASP.NET provides a new space and stage for the development of web applications. Although its performance has greatly improved, it is necessary to develop high-performance web applications. Setting and modification, this article proposes a number of improvements and techniques. However, since ASP.NET is still in development, its improved technology has to be further studied.

转载请注明原文地址:https://www.9cbs.com/read-10324.html

New Post(0)