13 asp.net session19

27
Developing Web Applications Using ASP.NET 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 Objectives

Upload: vivek-chan

Post on 14-Apr-2017

91 views

Category:

Education


1 download

TRANSCRIPT

Page 1: 13 asp.net session19

Developing Web Applications Using ASP.NET

In this session, you will learn to:Describe the Page Scripting Object ModelExplain how to use tracing and instrumentation to monitor and improve the performance of a Web applicationDescribe ASP.NET 2.0 caching techniquesExplain how asynchronous processing can lead to improved performance for Web applicationsDescribe strategies for dealing with session state management issues when deploying Web applications in a Web farm environmentAccess Page Scripting Object Model functionalityImplement tracing and instrumentation in Web applicationsImplement ASP.NET 2.0 caching techniques

Objectives

Page 2: 13 asp.net session19

Developing Web Applications Using ASP.NET

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.

The Page Scripting Object Model

Page 3: 13 asp.net session19

Developing Web Applications Using ASP.NET

Client–side 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.

The Page Scripting Object Model (Contd.)

Page 4: 13 asp.net session19

Developing Web Applications Using ASP.NET

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.

The Page Scripting Object Model (Contd.)

Page 5: 13 asp.net session19

Developing Web Applications Using ASP.NET

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:

RegisterClientScriptBlockRegisterClientScriptIncludeRegisterStartupScriptRegisterOnSubmitStatement

The Page Scripting Object Model (Contd.)

Page 6: 13 asp.net session19

Developing Web Applications Using ASP.NET

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);

}

The Page Scripting Object Model (Contd.)

Page 7: 13 asp.net session19

Developing Web Applications Using ASP.NET

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 callbackA method that receives the callback from the server codeA method that calls the helper method

The Page Scripting Object Model (Contd.)

Page 8: 13 asp.net session19

Developing Web Applications Using ASP.NET

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”;

}

The Page Scripting Object Model (Contd.)

Page 9: 13 asp.net session19

Developing Web Applications Using ASP.NET

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);

}

The Page Scripting Object Model (Contd.)

Page 10: 13 asp.net session19

Developing Web Applications Using ASP.NET

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’);” />

The Page Scripting Object Model (Contd.)

Page 11: 13 asp.net session19

Developing Web Applications Using ASP.NET

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>

The Page Scripting Object Model (Contd.)

Page 12: 13 asp.net session19

Developing Web Applications Using ASP.NET

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.

Tracing and Instrumentation in Web Applications

Page 13: 13 asp.net session19

Developing Web Applications Using ASP.NET

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>

Tracing and Instrumentation in Web Applications (Contd.)

Page 14: 13 asp.net session19

Developing Web Applications Using ASP.NET

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>

Tracing and Instrumentation in Web Applications (Contd.)

Page 15: 13 asp.net session19

Developing Web Applications Using ASP.NET

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”%>

ASP.NET 2.0 Caching Techniques

Page 16: 13 asp.net session19

Developing Web Applications Using ASP.NET

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”%>

ASP.NET 2.0 Caching Techniques (Contd.)

Page 17: 13 asp.net session19

Developing Web Applications Using ASP.NET

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.

Asynchronous Processing in Web Applications

Page 18: 13 asp.net session19

Developing Web Applications Using ASP.NET

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 callbackBy 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.

Asynchronous Processing in Web Applications (Contd.)

Page 19: 13 asp.net session19

Developing Web Applications Using ASP.NET

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 RequestsTransactions PendingRequests Timed Out

ASP.NET Performance Counters

Page 20: 13 asp.net session19

Developing Web Applications Using ASP.NET

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.

Web Farm Development Considerations

Page 21: 13 asp.net session19

Developing Web Applications Using ASP.NET

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 ServiceMicrosoft 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.

Web Farm Development Considerations (Contd.)

Page 22: 13 asp.net session19

Developing Web Applications Using ASP.NET

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.

Web Farm Development Considerations (Contd.)

Page 23: 13 asp.net session19

Developing Web Applications Using ASP.NET

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.

Demo: Optimizing Web Application Performance

Page 24: 13 asp.net session19

Developing Web Applications Using ASP.NET

Solution:You need to perform following tasks:

1. Access the Page Scripting Object Modela. 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.

Demo: Optimizing Web Application Performance (Contd.)

Page 25: 13 asp.net session19

Developing Web Applications Using ASP.NET

2. Implement ASP.NET Caching Techniquesa. 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.

3. Implement Tracing and Instrumentation Techniques in Web Applications

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

application.

Demo: Optimizing Web Application Performance (Contd.)

Page 26: 13 asp.net session19

Developing Web Applications Using ASP.NET

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.

Summary

Page 27: 13 asp.net session19

Developing Web Applications Using ASP.NET

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.

Summary (Contd.)