Вы находитесь на странице: 1из 27

Developing Web Applications Using ASP.

NET
Objectives In this session, you will learn to:
Describe the Page Scripting Object Model Explain how to use tracing and instrumentation to monitor and improve the performance of a Web application Describe ASP.NET 2.0 caching techniques Explain how asynchronous processing can lead to improved performance for Web applications Describe strategies for dealing with session state management issues when deploying Web applications in a Web farm environment Access Page Scripting Object Model functionality Implement tracing and instrumentation in Web applications Implement ASP.NET 2.0 caching techniques

Ver. 1.0

Slide 1 of 27

Developing Web Applications Using ASP.NET


The Page Scripting Object Model ASP.NET is a server-side programming model. Client side code can be useful to respond quickly to the user action, if it can be completed without extra information from the server. To respond to a user action, the browser needs to post the page back across the Internet for processing. This slows down the application significantly.

Ver. 1.0

Slide 2 of 27

Developing Web Applications Using ASP.NET


The Page Scripting Object Model (Contd.) Clientside script can be added to an ASP.NET page by using <script> tags, without the runat=server attribute. Client-script generated by ASP.NET controls does not interfere with any script added using <script> tag. You can also add client-side script to a page by using server-side code. Creating client script in server code is useful when:
Contents of the client script depend on information that is not available until run time. The client script needs to be executed when the page finishes loading. The client script needs to be executed when users submit the page.
Ver. 1.0

Slide 3 of 27

Developing Web Applications Using ASP.NET


The Page Scripting Object Model (Contd.) An attribute added to an ASP.NET server control which is not defined by their classes is processed by the browser. This feature can be used to define client-side event handlers for controls. A server control can be referred in the client-side script by using its ID property as set in the server-side code. A server control can also be referred in the client-side script by setting the ClientID property of the control.

Ver. 1.0

Slide 4 of 27

Developing Web Applications Using ASP.NET


The Page Scripting Object Model (Contd.) Adding Client Scripts Dynamically:
In case client-side script depends on information available only at run time, it can be generated and added in the page at run time by using the System.Web.UI.ClientScriptManager class. To add client script to a page dynamically, you need to call one of the following methods:
RegisterClientScriptBlock RegisterClientScriptInclude RegisterStartupScript RegisterOnSubmitStatement

Ver. 1.0

Slide 5 of 27

Developing Web Applications Using ASP.NET


The Page Scripting Object Model (Contd.)
The following code adds a script that will execute in the browser when the user attempts to submit a form:
void Page_Load () { String scriptText = return confirm( Do you want to submit the page?); ClientScript.RegisterOnSubmitStatement (this.GetType(), ConfirmSubmit, scriptText); }

Ver. 1.0

Slide 6 of 27

Developing Web Applications Using ASP.NET


The Page Scripting Object Model (Contd.) Client Callbacks Without Postbacks:
Client callbacks provide a way to obtain information from the server without executing the entire page again. In a client callback, client-side code on the page invokes a server-side method without performing a postback. This method can return results back to the client-side script. An ASP.NET page that implements client callback must:
Implement the ICallbackEventHandler interface. Include a method that implements the RaiseCallbackEvent method from the interface. Contain the following client script functions:
A helper method that performs the callback A method that receives the callback from the server code A method that calls the helper method

Ver. 1.0

Slide 7 of 27

Developing Web Applications Using ASP.NET


The Page Scripting Object Model (Contd.) Implementing the ICallbackEventHandler Interface:
A page can implement the ICallbackEventHandler interface by defining the class for the page as: public partial class CallBack_class: System.Web.UI.ICallbackEventHandler

Implementing the RaiseCallbackEvent Method:


The following is a sample implementation of the RaiseCallbackEvent method of the ICallbackEventHandler interface: public string RaiseCallbackEvent (string eventArgument) { Return eventArgument + new value; }

Ver. 1.0

Slide 8 of 27

Developing Web Applications Using ASP.NET


The Page Scripting Object Model (Contd.) Sending the Callback:
The following example shows how to dynamically create a function that invokes the callback: void Page_Load(object sender, EventArgs e) { string cbReference = Page.ClientScript.GetCallbackEventReference( this, arg, ReceiveServerData, context); string callbackScript = function CallTheServer(arg, context) { + cbReference + ; }; Page.ClientScript.RegisterClientScriptBlock( this.GetType(), CallTheServer, callbackScript, true);
Ver. 1.0

Slide 9 of 27

Developing Web Applications Using ASP.NET


The Page Scripting Object Model (Contd.)
The following example shows how an HTML input button control can initiate the callback process: <input type=button id=MyButton onclick=return CallTheServer(Some Data); />

Ver. 1.0

Slide 10 of 27

Developing Web Applications Using ASP.NET


The Page Scripting Object Model (Contd.) Receiving the Callback:
The following example shows how to create a client-side method for receiving a callback: <script type=text/javascript> function ReceiveServerData(rvalue, context) { span1.innerText = Return value = + rvalue; } </script>

Ver. 1.0

Slide 11 of 27

Developing Web Applications Using ASP.NET


Tracing and Instrumentation in Web Applications Tracing features enable gathering of information for debugging and performance tuning. Tracing can be enabled for an ASP.NET page by adding trace=true attribute to the <%@page%> directive. Diagnostic information, mostly related to the performance of the application is generated and displayed at the bottom of the rendered page. Custom instrumentation data can be added to the trace information displayed by calling methods on the System.Web.TraceContext class. In addition to displaying messages that you send, trace information includes the times at which the messages were received. This can be used to diagnose parts of the code that execute slowly.
Ver. 1.0

Slide 12 of 27

Developing Web Applications Using ASP.NET


Tracing and Instrumentation in Web Applications (Contd.) The classes in the System.Diagnostics namespace allow you to implement a tracing system. Trace statements must be added at strategic points within the code to generate tracing information. Trace Listener objects receive tracing output and write it to logs, text files, or the screen. ASP.NET tracing information can also be sent to a System.Diagnostic listener by using Web.config file:
<system.web> <trace writeToDiagnosticsTrace=true/> <CustomErrors mode=off/> </system.web>

Ver. 1.0

Slide 13 of 27

Developing Web Applications Using ASP.NET


Tracing and Instrumentation in Web Applications (Contd.) The System.Diagnostics tracing information can be routed to the browser by the using Web.config file:
<system.diagnostics> <trace> <listeners> <add name = WebPageTraceListener type=System.Web.WebPageTraceListener, System.Web, Version =2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a/> </listeners> </trace> </system.diagnostics>
Ver. 1.0

Slide 14 of 27

Developing Web Applications Using ASP.NET


ASP.NET 2.0 Caching Techniques Objects and pages that require substantial resources can be cached for reuse. The application cache enables caching of objects and data for reuse. Storing data in the application cache is similar to storing data in the Application object. Objects in the cache are volatile and can be removed without warning. Another type of cache, the Page Output cache stores rendered pages in server memory. You can enable Page Output caching by including the <$@ OutputCache %> directive on the page: <%@ OutputCache Duration=60 VaryByParam=None%>
Ver. 1.0

Slide 15 of 27

Developing Web Applications Using ASP.NET


ASP.NET 2.0 Caching Techniques (Contd.) In some cases, it is not feasible to cache the entire page. A page can be cached partially by:
Adding <%@OutputCache%> directive in the control that needs to be cached. Placing <%@OutputCache%> directive on the page and marking certain sections exempt from caching by surrounding these sections with a Substitution control.

Multiple versions of a page can be cached by using varybyparam attribute in the <%@OutputCache%> directive:
<%@OutputCache Duration = 60 VaryByParam=City%>

Ver. 1.0

Slide 16 of 27

Developing Web Applications Using ASP.NET


Asynchronous Processing in Web Applications A Web page can be configured to execute some requests asynchronously. On a Web page, it can be specified that runtime should not necessarily wait for a long method to complete before continuing with the next piece of code. The Async property of the Page object is used to enable code on a Web page to run asynchronously:
<%@ page Async=true%>

To perform an asynchronous operation, the following methods need to be registered:


Begin: This method sets the operation running. End: This method responds when the operation has completed.

Ver. 1.0

Slide 17 of 27

Developing Web Applications Using ASP.NET


Asynchronous Processing in Web Applications (Contd.) Calling Web Services Asynchronously:
Web services can be hosted on servers remote from the one that hosts the Web application. Web services can be called asynchronously in case other operations do not depend on their result. There are two ways to call a Web service asynchronously:
By using a callback By using a WaitHandle

Loading XML Asynchronously:


XML files can be large and located remotely from the Web server. As a result, they may take a long time to load. XML files can be loaded asynchronously in the same way as a Web service.

Ver. 1.0

Slide 18 of 27

Developing Web Applications Using ASP.NET


ASP.NET Performance Counters Are used to capture performance data and log them in the Performance Logging database or Windows Performance monitor. ASP.NET supports the following types of performance counters:
System: Are exposed in the Windows Performance monitor as the ASP.NET performance counter object. Application: Are exposed as the ASP.NET Applications performance object.

The following are some of the performance counters:


Anonymous Requests Transactions Pending Requests Timed Out

Ver. 1.0

Slide 19 of 27

Developing Web Applications Using ASP.NET


Web Farm Development Considerations Response time of a Web application can be reduced by hosting it on a Web farm. A Web farm is group of servers configured to act as a single Web server. The servers in a Web farm can share the processing between them. You should consider using a Web farm if you have optimized your application fully and upgraded your server as much as possible but the application is still not responding quickly.

Ver. 1.0

Slide 20 of 27

Developing Web Applications Using ASP.NET


Web Farm Development Considerations (Contd.) Considerations for Building a Web Farm:
Using an External Session State Provider:
In a Web farm each request in a session can be directed to different servers in the farm. Therefore, it is not practical to store session state in memory of any of the servers. Two external session state providers can be used:
The ASP.NET State Service Microsoft SQL Server

Configuring the <MachineKey> tag in machine.config:


The <machineKey> tag configures the way encryption is used to protect forms authentication cookies and view state information. Encryption keys are generated automatically and are unique to each server. To access shared protected information in a Web farm, each server must use the same encryption keys.

Ver. 1.0

Slide 21 of 27

Developing Web Applications Using ASP.NET


Web Farm Development Considerations (Contd.) Deploying to a Web Farm:
Each server in a Web farm must have an identical copy of the application. Microsoft Windows Installer package is helpful to ensure consistency among different servers while deploying the application.

Ver. 1.0

Slide 22 of 27

Developing Web Applications Using ASP.NET


Demo: Optimizing Web Application Performance Problem Statement:
You are a developer in the Adventure Works organization, a fictitious bicycle manufacturer You have been asked to assist in creating a new Business-to-Consumer (B2C) Web application and a related Business-to-Employee (B2E) extranet portal. Decisions on the design of the application have already been made. You have been asked to carry out a number of specific tasks in order to implement various elements of this design. As part of the first phase of the B2C development, you have been asked to prototype various performance related techniques for the Web application.

Ver. 1.0

Slide 23 of 27

Developing Web Applications Using ASP.NET


Demo: Optimizing Web Application Performance (Contd.) Solution:
You need to perform following tasks:
1. Access the Page Scripting Object Model
a. Open the starter solution. b. Write the test code to set the focus to the Name text box when displaying the Online Survey page. c. Use the ClientScript object to inject client-side script that displays a message box onto the Web page. d. Use the ClientScript object to implement out-of-band callbacks. e. Add a client-side JavaScript function for receiving the result of the callback. f. Test the callbacks.

Ver. 1.0

Slide 24 of 27

Developing Web Applications Using ASP.NET


Demo: Optimizing Web Application Performance (Contd.)
2. Implement ASP.NET Caching Techniques
a. Implement page caching declaratively. b. Implement parameterized page caching in code. c. Prevent portions of a page from being cached by using a Substitution control. d. Implement Web page fragment caching by using a Web user control.

2. Implement Tracing and Instrumentation Techniques in Web Applications


a. b. c. d. Add and review tracing information on a Web page. Add event logging capabilities to the Web application. Add and manipulate performance counters for the Web application. Test the logging and performance counter management of the Web application.

Ver. 1.0

Slide 25 of 27

Developing Web Applications Using ASP.NET


Summary In this session, you learned that:
Client-side script can be added to an ASP.NET page by using <script> tags, without the runat=server attribute. System.Web.UI.ClientScriptManager class is used to generate and render client-side code at run time. To enable tracing for an ASP.NET page trace=true attribute can be added to the <%@Page%> directive. The classes in the System.Diagnostics namespace also provides a tracing system. The ASP.NET application cache enables the storing of objects and data for reuse. A page can be cached by including the <%@OutputCache%> directive on the page.

Ver. 1.0

Slide 26 of 27

Developing Web Applications Using ASP.NET


Summary (Contd.)
The Async property of the Page object enables code on the Web page to run asynchronously. Response time of a Web application can be improved by hosting it on a Web farm. The ASP.NET State Service or Microsoft SQL Server can be used as an external session state provider in a Web farm.

Ver. 1.0

Slide 27 of 27

Вам также может понравиться