Chapter 10 -improving Web Services Performance

zhaozj2021-02-16  64

Chapter 10 -improving Web Services Performance

Improving .NET Application Performance and Scalability

J. D. Meier, Srinath Vasireddy, Ashish Babbar, And Alex Mackmanmicrosoft Corporation

May 2004

Related Links

Home Page for Improving .NET Application Performance and Scalability

Chapter 5, Improving Managed Code Performance

Checklist: Web Services Performance

Send feedback to scale@microsoft.com

Patterns & Practices Library

Summary: This chapter focuses on design guidelines and techniques, such as state management, asynchronous invocation, serialization, threading, to help you develop efficient Web services This chapter also presents a formula for reducing thread contention and HTTP connections to increase the throughput for your. Web services.

Contents

ObjectivesOverviewHow to Use This ChapterArchitecturePrescriptive Guidance for Web Services, Enterprise Services, and .NET RemotingPerformance and Scalability IssuesDesign ConsiderationsImplementation ConsiderationsConnectionsThreadingOne-Way (Fire-and-Forget) CommunicationAsynchronous Web MethodsAsynchronous InvocationTimeoutsWebMethodsSerializationCachingState ManagementBulk Data TransferAttachmentsCOM InteropMeasuring and Analyzing Web Services PerformanceWeb Service EnhancementsSummaryAdditional Resources

Objectives

Identify top Web services performance issues. Design scalable Web services that meet your performance objectives. Improve serialization performance. Configure the HTTP runtime for optimum performance. Improve threading efficiency. Evaluate and choose the most appropriate caching mechanism. Decide when to maintain state. Evaluate and Choose The Most Appropriate Bulk Data Transfer Mechanism.

Overview

Services are the ideal communication medium for distributed applications. You should build all of your services using Web services and then, if necessary, use Enterprise Services or Microsoft? .NET remoting within the boundaries of your service implementation. For example, you might need to use Enterprise Services for distributed transaction support or object pooling.Web services are ideal for cross-platform communication in heterogeneous environments because of their use of open standards such as XML and Simple Object Access Protocol (SOAP). However, even in a closed environment where Both Client and Server Systems Use The .NET Framework, Ease of Deployment and Maintenance Make Web Services a Very Attractive Approach.

This chapter begins by examining the architecture of ASP.NET Web services, and then explains the anatomy of a Web services request from both the client and server-side perspectives. You need a solid understanding of both client and server to help you identify and address typical Web services performance issues. An understanding of Web services architecture will also help you when you configure the HTTP runtime to optimize Web services performance. The chapter then presents a set of important Web services design considerations, followed by a series of sections that address the Top Web Services Performance Issues.

HOW to use this chapter

TO GET The MOST OUT OF THIS Chapter:

Jump to topics or read from beginning to end. The main headings in this chapter help you to locate the topics that interest you. Alternatively, you can read the chapter from beginning to end to gain a thorough appreciation of performance and scalability design issues. Use the checklist Use. "checklist: Web Services Performance" in the "Checklists" section of this guide to quickly view and evaluate the guidelines presented in this chapter Use the "Architecture" section of this chapter to learn how Web services work By understanding.. Web services architecture, you can make better design and implementation choices. Use the "Design Considerations" section of this chapter. This section helps you to understand the higher-level decisions that affect implementation choices for Web services code. Read Chapter 6, "Improving ASP.NET Performance "Many of the Performance Optimizations Described in Chapter 6," Improving Asp.Net Performance "- Such As Tuning The Thread Pool and Designing and Impleme nting efficient caching - also apply to ASP.NET Web services development Read Chapter 13,... "Code Review: .NET Application Performance" See the "Web Services" section of Chapter 13 for specific guidance Measure your application performance Read the "Web Services "and" .NET Framework Technologies "sections of Chapter 15," Measuring .NET Application Performance "to learn about key metrics that you can use to measure application performance. It is important that you are able to measure application performance so that you can Test Your Application Performance. Read Chapter 16, "

Testing .NET Application Performance "to learn how to apply performance testing to your application. It is important that you apply a coherent testing process and that you are able to analyze the results. Tune your application performance. Read the" Web Services "section of Chapter 17, "Tuning .NET Application Performance" to Learn How to Resolve Performance Issues Identified Through The Use of Tuning Metrics.Architecture

The server-side infrastructure is based on ASP.NET and uses XML serialization. When the Web server processes an HTTP request for a Web service, Internet Information Services (IIS) maps the requested extension (.asmx) to the ASP.NET Internet server application programming interface (ISAPI) extension (Aspnet_isapi.dll). The ASP.NET ISAPI extension then forwards the request to the ASP.NET worker process, where it enters the request processing pipeline, which is controlled by the HttpRuntime object. See Figure 10.1 For An Illustration of The Web Services Architecture and Request Flow.

Figure 10.1: ASP.NET Web Services Architecture and Request Flow

The request is initially passed to the HttpApplication object, followed by the series of registered HttpModule objects. HttpModule objects are registered in the system-wide Machine.config file or in the

Section of an application-specific Web.config file.

Httpmodule Objects Handle Authentication, Authorization, Caching, And Other Services.

After passing through the HTTP modules in the pipeline, HttpRuntime verifies that the .asmx extension is registered with the WebServiceHandlerFactory handler. This creates an HTTP handler, an instance of a type that derives from WebServiceHandler, which is responsible for processing the Web services request. The Http Handler Uses Reflection To Translate SOAP Messages Into Method Invocations. WebServiceHandler Is Located in The System.Web.Services.Protocols Namespace.Client-Side Proxy Classes

On the client side, proxy classes provide access to Web services. Proxy classes use XML serialization to serialize the request into a SOAP message, which is then transported using functionality provided by the System.Net namespace.

You can use the Wsdl.exe tool to automatically generate the proxy class from the Web Services Description Language (WSDL) contract file. Depending on the bindings specified in the WSDL, the request issued by the proxy may use the HTTP GET, HTTP POST, OR http soap protocols.

The Proxy Class Is Derived from One of the Following Bases:

System.web.services.protocols.httpgetClientProtocol System.Web.Services.protocols.httppostClientProtocol System.Web.Services.protocols.SoAphttpClientProtocol

These all derive from System.Web.Services.Protocols.HttpWebClientProtocol, which in turn derives from the System.Web.Services.Protocols.WebClientProtocol base class in the inheritance chain. WebClientProtocol is the base class for all automatically generated client proxies for ASP. NET Web Services, And, AS A Result, your proxy class inherits Many of its methods and profits.

Prescriptive Guidance for Web Services, Enterprise Services, and .NET Remoting

Services are the preferred communication technique to use across application boundaries, including platform, deployment, and trust boundaries. You can implement services today by using Web services or Web Services Enhancements (WSE). Although WSE provides a rich set of features, you should evaluate whether or not you can accept the WSE support policy. Enterprise Services provides component services such as object pooling, queued components, a role-based security model and distributed transactions, and should be used as an implementation detail within your service when you need those features . .NET Remoting is preferred for cross-applin.com

When you design distributed applications, use the services approach whenever possible. Although object orientation provides a pure view of what a system should look like and is effective for producing logical models, an object-based approach can fail to consider real-world factors, such AS Physical Distribution, Trust Boundaries, And Network Communication, AS Well As Nonfunctional Requirements, Such As Performance and Security.

Table 10.1 Summarizes Some Key Differences Between Object Orientation and Service Orientation.

Table 10.1: Object Orientation vs. Service Orientation

Object OrientationService OrientationAssumes a homogeneous platform and execution environment.Assumes a heterogeneous platform and execution environment.Shares types, not schemas.Shares schemas, not types.Assumes cheap, transparent communication.Assumes variable cost, explicit communication.Objects are linked: object identity and lifetime are maintained by the infrastructure.Services are autonomous: security and failure isolation are a must.Typically requires synchronized deployment of both client and server.Allows continuous, separate deployment of client and server.Is easy to conceptualize and thus provides a natural model to follow.Builds on ideas from component software and distributed objects. Dominant theme is to manage / reduce sharing between services.Provides no explicit guidelines for state management and ownership. Owns and maintains state or uses the reference state.Assumes a predictable sequence, timeframe, And Outcome of Invocations.assumes Message-Oriented, Potentially Assy nchronous, and long-running communications.Goal is to transparently use functions and types remotely.Goal is to provide inter-service isolation and wire interoperability based on standards.Application Boundaries

. Common application boundaries include platform, deployment, trust, and evolution (. Evolution refers to whether or not you develop and upgrade applications together) When you evaluate architecture and design decisions that affect your application boundaries, consider the following:

Objects and Remote Procedure Calls (RPC) Are Appropriate Withnin Boundaries. Services are appropriate across and with forn boundaries.

Recommendations for Web Services, Enterprise Services, and .NET Remoting

When you are working with ASP.NET Web services, Enterprise Services, and .NET remoting, Microsoft recommends that you: Build services by using ASP.NET Web Services Enhance your ASP.NET Web services with WSE if you need the WSE feature set. and you can accept the support policy Use object technology, such as Enterprise Services or .NET remoting, within a service implementation Use Enterprise Services inside your service boundaries in the following scenarios..:

You need the Enterprise Services feature set (such as object pooling; declarative, distributed transactions; role-based security; and queued components). You are communicating between components on a local server and you have performance issues with ASP.NET Web services or WSE . Use .NET Remoting Inside your service Boundaries in The Following Scenarios:

You need in-process, cross-application domain communication. Remoting has been optimized to pass calls between application domains extremely efficiently. You need to support custom wire protocols. Understand, however, that this customization will not port cleanly to future Microsoft implementations.

Caveats

WHEN You Work with ASP.NET Web Services, Enterprise Services, or .NET Remoting, Consider The Following Caveats:

If you use ASP.NET Web services, avoid or abstract your use of low-level extensibility features such as the HTTP Context object. If you use .NET remoting, avoid or abstract your use of low-level extensibility such as .NET remoting sinks And Custom Channels. If You Use Enterprise Services, Avoid Passing Object References Inside Enterprise Services. Also, Do Not Use Com Apis. Instead, Use Types from The System.EnterpriseServices Namespace.

More information

For guidelines on how to make .NET Enterprise Services components execute as quickly as C COM components, see the MSDN? Article, ".NET Enterprise Services Performance," at http://msdn.microsoft.com/library/en-us/ dncomser / html / entsvcperf.asp. For more information on Enterprise Services, see Chapter 8, "Improving Enterprise Services Performance" For more information on remoting, see Chapter 11, "Improving Remoting Performance" Performance and Scalability Issues

The main issues that can adversely affect the performance and scalability of your Web services are summarized in the following list. Subsequent sections in this chapter provide strategies and technical information to prevent or resolve each of these issues.

Incorrect communication mechanism Currently, there are three main technologies for remoting a method call:.. Enterprise Services, .NET remoting, and ASP.NET Web services The best choice depends upon various factors, including the source and target platforms, whether you need to communicate across an intranet or the Internet, whether you require additional services such as distributed transactions, your security requirements, deployment considerations (such as whether your communication must pass through a firewall), other port limitations, and so on.

Web services. Use Web services to build your services. Enterprise Services. If you use Web services to build your services, you may still need to use Enterprise Services within your service implementation. For example, you may need it to support distributed transactions or if you want to use object pooling. .NET remoting. use remoting for same-process, cross-application domain communication or for remote communication if you need to integrate with a legacy protocol. If you use remoting, avoid custom proxies, custom sinks, and using contexts. This helps to avoid compatibility issues with future communication technologies. Chatty calls. Network round trips to and from a Web service can be expensive. This issue is magnified if clients need to issue multiple requests to a Web service to complete a single logical Operation. Improper choice of parameters. Your choice of parameters Depends Upon Various Factors, Such As Interoperability, The Varying Platforms Used by The Clients, Maintainabil ity, the type of encoding format used, and so on. Improper choice of parameters can lead to a number of issues, including increased serialization costs and potential versioning problems for the Web service (for example where a custom type is updated). Where possible , you should use primitive types. If interoperability is an issue, consider using the XmlElement and XmlDocument types and choose types specific to your application, such as an Employee or Person class. Serialization. Serializing large amounts of data and passing it to and from Web Services can Cause Performance-Related Issues, Including Network Congestion and Excessive Memory and Processor Overhead.

Other issues that affect the amount of data passed across the wire include improper data transfer strategies for large amounts of data Selecting an appropriate data transfer strategy -. Such as using a SOAP extension that performs compression and decompression or offloading data transfer to other services - is critical to the performance of your Web services solution. Improper choice of encoding format. You can use either literal or SOAP encoding. SOAP encoding involves more SOAP-processing overhead as compared to literal encoding. Lack of caching or inefficient caching. In many situations, application or perimeter caching can improve Web services performance. Caching-related issues that can significantly affect Web services performance include failure to use caching for Web methods, caching too much data, caching inappropriate data, and using inappropriate expiration settings. Inefficient state management. Inefficient State Management Design in Web Services Can Lead to Scalability Bo ttlenecks because the server becomes overloaded with state information that it must maintain on a per-user basis. Common pitfalls for Web services state management include using stateful Web methods, using cookie container-based state management, and choosing an inappropriate state store. The most scalable Web services maintain no state. Misuse of threads. It is easy to misuse threads. For example, you might create threads on a per-request basis or you might write code that misuses the thread pool. Also, unnecessarily implementing a Web method asynchronously .

On the client side, consumers of Web services have the option of calling Web services asynchronously or synchronously. Your code should call a Web service asynchronously only when you want to avoid blocking the client while a Web service call is in progress. If you are not careful, you can use a greater number of worker and I / O threads, which negatively affects performance It is also slower to call a service asynchronously;. therefore, you should avoid doing so unless your client application needs to do something else while the service is invoked. Inefficient Web method processing. A common example of inefficient processing is not using a schema to validate input upfront. This issue can be significant because the Web method may de-serialize the incoming message and then throw exceptions later on while processing the input Data.Design Considances

To Help Ensure That You Create Efficient Web Services, There a Number of Issues That You Must Make At Design Time. The Following Are Major Considerations:

Design chunky interfaces to reduce round trips. Prefer message-based programming over RPC style. Use literal message encoding for parameter formatting. Prefer primitive types for Web services parameters. Avoid maintaining server state between calls. Consider input validation for costly Web methods. Consider your Approach to Caching. Consider Approaches for Bulk Data Transfer and attachments. Avoid Calling Local Web Services.

Design Chunky Interfaces To Reduce Round Trips

Design chunky interfaces by exposing Web methods that allow your clients to perform single logical operations by calling a single Web method. Avoid exposing properties. Instead, provide methods that accept multiple parameters to reduce roundtrips.Do not create a Web service for each of your business Objects. A Web Service Should Wrap A Set of Business Objects. Use Web Services To Abstract The CHESE Objects and Increase The Chunkiness of Your Calls.

Prefer Message-Based Programming Over RPC STYLE

You can design Web services by using either of two programming models:.. Messaging style and RPC style The RPC style is based on the use of objects and methods Web methods take object parameters to do the processing, and then return the results This style. GENERALLY RELIES ON MAKING MULTIPLE Web Method Calls to Complete A Single Logical Operation, AS Shown In The Following Code Snippet.

// Client Calling a Web Service Serv.sendItemStobePurchased (Array [] Items; serv.shippingaddress (String Address); serv.checkout ();

The messaging style does not focus on objects as parameters. It is based on a data contract (schema) between the Web service and its clients. The Web service expects the message to be XML that conforms to the published data contract.

// Client String MSG = "

...

"; MyMethod (MSG); // server [webmethod] void mymethod (String MSG) {..

This approach allows you to package and send all parameters in a single message payload and complete the operation with a single call, thus reducing chatty communication The Web service may or may not return results immediately;. Therefore, the clients do not need to wait for RESULTS.

Use literal message encoding for parameter formter Formatting

The encoded formatting of the parameters in messages creates larger messages than literal message encoding (literal message encoding is the default). In general, you should use literal format unless you are forced to switch to SOAP encoding for interoperability with a Web services platform that does Not support The Literal Format.Prefer Primitive Types for Web Services Parameters

There Are Types That You CAN Pass To Web Services:

Strongly typed. These include .NET types such as double and int, and custom objects such as Employee, Person, and so on. The advantage of using strongly typed parameters is that .NET automatically generates the schema for these types and validates the incoming values for you. Clients use the schema to construct appropriately formatted XML messages before sending them. Loosely typed. These parameters use the string type. Note that you should not pass XML documents as string parameters because the entire string then needs to be XML encoded. for example, needs to be converted to and so on. Instead, you should use either an XmlElement parameter or implement IXmlSerializable. The latter is the most efficient and works well for large data sizes, regardless of which encoding style you Use, You Should Prefer Simple Types Like Int, Double, And String for Web Services Parameters. Use of primitive type leads to reduced serialization and automatic and effect validatio n by The .NET Framework.

Avoid Maintaining Server State Between Calls

Maintaining per-caller state in memory on the server limits scalability because the state consumes server resources. As an alternative, you can pass state back and forth between the client and Web service. Although this approach enables you to scale your service, it does add Performance Overhead - Including The Time Taken To Serialize, Transmit, Parse, And De-Serialize The State with Each Call.Consider Input Validation for Costly WebMMETHODS

If you have a Web method that performs costly and time-consuming processing, consider validating the Web method input before processing it. It can be more efficient to accept the validation overhead to eliminate unnecessary downstream processing. However, unless you are likely to receive invalid input frequently, you should probably avoid schema validation due to the significant overhead that it introduces. you need to assess your specific situation to determine whether or not schema validation is appropriate.

You can validate input data either by using SOAP extensions or by using separate internal helper methods that your Web methods call. The advantage of using SOAP extensions is that they permit you to separate your validation code from your business logic. If there is any schema change In The Future, The Extension Can Change Independently of The WebMMETHOD.

Another option is to use the xmlvalidatingreader class to perform Schema-based Validation, AS Shown In The Following Code Snippet.

[WebMethod] public void ValidateCreditCard (string xmlCardInfo) {try {// Create and load a validating reader XmlValidatingReader reader = new XmlValidatingReader (xmlCardInfo, XmlNodeType.Element, null); // Attach the XSD schema to the reader reader.Schemas.Add ( "urn: CardInfo-schema", @ "http: //localhost/Card/Cardschema.xsd"); // Set the validation type for XSD schema // XDR schemas and DTDs are also supported reader.ValidationType = ValidationType.. schema; // Create and register an event handler to handle validation errors reader.ValidationEventHandler = new ValidationEventHandler (ValidationErrors); {...} // Process the input data while (reader.Read ()) // Validation completed successfully} catch {...}} // Validation error event handler private static void ValidationErrors (object sender, ValidationEventArgs args) {// error details available from args.Message...} Consider Your Approach to Caching

You can greatly enhance Web services performance by caching data. With ASP.NET Web services, you can use many of the same caching features that are available to ASP.NET applications. These include ASP.NET output caching, HTTP response caching, and ASP .NET Application Caching.

In common with any caching solution, your caching design for a Web service must consider issues such as how frequently the cached data needs to be updated, whether or not the data is user-specific or application-wide, what mechanism to use to indicate that .

Consider Approaches for Bulk Data Transfer and Attachments

You can use the following approaches to optimize the performance of bulk data transfer:... Chunking With this approach, you use fixed-size byte arrays to send the data one chunk at a time Offloading the transfer With this approach, you return a URL from your Web service which points to the file to be downloaded. Compression. you can use a SOAP extension to compress the SOAP messages before transmitting them. This helps when you are constrained primarily by network bandwidth or latency.

To Handle Attachments, Your Options Include:

WS-Attachments Base 64 Encoding SOAP Message Transmission Optimization Mechanism (MTOM)

More information

For More Information About these approaches, See The "Bulk Data Transfer" and "attachments" Sections Later in this chapter.

Avoid Calling Local Web Services

Web services located on the same computer as a client ASP.NET application share the same thread pool with the ASP.NET application. Therefore, the client application and the Web service share the same threads and other related resources, such as CPU for request processing . Calling a local Web service also means that your request travels through the entire processing pipeline and incurs overhead, including serialization, thread switching, request queuing, and de-serialization.

In addition, the maxconnection attribute of Machine.config has no affect on the connection limit for making calls to local Web services. Therefore, local Web services always tend to give preference to the requests that come from the local computer over requests that come from other Machines. this Degrades The Throughput of the Web Service for Remote Clients.

There area TWO Main Approaches To Solving this question

Factor out the Web services business logic into a separate assembly, and call the assembly from the client application as well as the Web service. Load the Web services assembly directly and call its methods. This approach is not as intuitive as the first.More Information

For IIS 6.0 - specific deployment mitigation, refer to "ASP.NET Tuning" in Chapter 17, "Tuning .NET Application Performance" For more information about how to structure your application properly, refer to "Application Architecture for .NET: Designing Applications and Services "on msdn at http://msdn.microsoft.com/library/en-us/dnbda/html/distapp.asp.

Implementation considances

. When you move from application design to development, consider the implementation details of your Web services Important Web services performance measures include response times, speed of throughput, and resource management:

You can reduce request times and reduce server load by caching frequently used data and SOAP responses. You can improve throughput by making effective use of threads and connections, by optimizing Web method serialization, and by designing more efficient service interfaces. Tune thread pooling to reduce contention and increase CPU utilization. to improve connection performance, configure the maximum limit of concurrent outbound calls to a level appropriate for the CPU performance. You can improve resource management by ensuring that shared resources, such as connections, are opened as late as possible and Closed as soon as possible, and also by Not maintaining Server State Between Calls.

BY FOLLOWING BEST PRACTICE IMPLEMENTATION THE FOR Web Services. The Following Sections Highlight Performance Considerations for Web Services Features and Scenarios.Connections

When you call Web services, transmission control protocol (TCP) connections are pooled by default. If a connection is available from the pool, that connection is used. If no connection is available, a new connection is created, up to a configurable limit. there is always a default unnamed connection pool. However, you can use connection groups to isolate specific connection pools used by a given set of HTTP requests. to use a separate pool, specify a ConnectionGroupName when you make requests. If you do not specify a connection group, the default connection pool is used. to use connections efficiently, you need to set an appropriate number of connections, determine whether connections will be reused, and factor in security implications.

The Following Recommendations Improve Connection Performance:

Configure the maxconnection attribute. Prioritize and allocate connections across discrete Web services. Use a single identity for outbound calls. Consider UnsafeAuthenticatedConnectionSharing with Windows Integrated Authentication. Use PreAuthenticate with Basic authentication.

Configure the maxconnection attribute

.

Note ??? This setting does not apply to local requests (requests that originate from ASP.NET applications on the same server as the Web service). The setting applies to outbound connections from the current computer, for example, to ASP.NET applications And Web Services Calling Other Remote Web Services.

The default setting for maxconnection is two per connection group. For desktop applications that call Web services, two connections may be sufficient. For ASP.NET applications that call Web services, two is generally not enough. Change the maxconnection attribute from the default of 2 TO (12 Times The Number of CPUS) as a starting point.

Note that 12 connections per CPU is an arbitrary number, but empirical evidence has shown that it is optimal for a variety of scenarios when you also limit ASP.NET to 12 concurrent requests (see the "Threading" section later in this chapter). However YOULD VALIDATE The Appropriate Number of Connections for Your Situation.

Increasing the maxconnection attribute results in increased thread pool and processor utilization. With the increase in the maxconnection value, a higher number of I / O threads will be available to make outbound concurrent calls to the Web service. As a result, you process incoming HTTP Requests more quickly.

Before Making The Change

You should consider increasing the connections only if you have available CPU. You should always check processor utilization before increasing the attribute because increasing the attribute results in more work for the processor, as described above. For this reason, increasing this attribute makes sense only when Your Processor Utilization IS Below The Threshold Limits (Usually Less Than 75 Percent Utilization).

For more information, see the "threading" section next chapter.

Evaluating the change

Changing the attribute may involve multiple iterations for tuning and involves various trade-offs with respect to thread pool utilization Therefore, the changes in the maxconnection attribute may require changes to other thread pool -. Related configuration attributes, such as maxWorkerThreads and maxIoThreads.When you load test your application after making the configuration changes, you should monitor CPU utilization and watch the ASP.NET Applications / Requests / Sec and ASP.NET Applications / Requests in Application Queue performance counters. Requests in Application Queue should decrease while Requests / Sec and CPU Utilization Should Increase.

Prioritize and Allocate Connections Across Discrete Web Services

Enumerate and prioritize the Web Services you call. Allocate More Connections. You Specify Each Web Service By Using The Address Attribute as Follows.

For example, if your application Typically Makes More Requests to WebServicea Than WebServiceB, You CAN Dedicate More Connections, As Shown In The Example Above.

Use a single Identity for Outbound Calls

Use a single trusted identity for making Web services calls where you can. This helps limit the number of separate connection pools. Although you may need to create separate pools of connections for different discrete Web services, avoid creating pools per user. If you need to Create Pools Per User, The Specify A ConnectionGroupname WHEN You Call The Web Service, But Be aware That Hurts Performance and Leads to a large number of pools.

The connection pool your call uses is not determined by the identity of the caller. The ConnectionGroupName determines which connection pool is used. If separate identities use the same ConnectionGroupName, they use the same pool of connections, as shown in the following code snippet./ / Create a secure group name ... serv = new WebService1 ();.. // Set the PreAuthenticate property to send the authenticate request in first go serv.PreAuthenticate = true; // Set the client side credentials ICredentials conCredentials = new NetworkCredential ( " UserId "," Password "," NPSTest "); serv.Credentials = conCredentials; // Do not allow the server to auto redirect as this may compromise security serv.AllowAutoRedirect = false; // Use the same connectionGroup Name for all the calls Serv.connectionGroupName = "SameForlUsers";

You May, the origin, you.

If ASP.NET calls Web services that allow anonymous callers, connections from the default connection pool are used. This is the default behavior unless you specify a ConnectionGroupName, as shown in above example.

Consider UnsafeAuthenticatedConnectionSharing with Windows Integrated Authentication

If your ASP.NET application calls a Web service that uses Microsoft Windows? Integrated authentication, consider enabling UnsafeAuthenticatedConnectionSharing. By default, when you connect using Windows Integrated authentication, connections are opened and closed per request. This means that connections are not pooled by default . By enabling UnsafeauthenticatedConnectionsHARING, You Keep Connections Open So They Can Be Reused.

Consider the following guidelines: If you are in a trusted environment and you connect using a single trusted identity, consider improving performance by setting UnsafeAuthenticatedConnectionSharing to true // set the UnsafeAuthenticatedConnectionSharing to true myWebService.UnsafeAuthenticatedConnectionSharing = true; NetworkCredential myCred = new NetworkCredential ( ". Usera "," Passworda "," Domaina "); CredentialCache mycache = new credentialcache (); mycache.add (New Uri (" http://someserver/ws/service1.asmx ")," ntlm ", mycred; mywebservice .Credentials = myCache; myWebService.ConnectionGroupName = "SameName"; string result = myWebService.HelloWorld (); // as the ConnectionGroupName property is same for different client requests // only the first connection from above gets authenticated // the request below reuses The Connection from Above Mycred = New Network, "Passwordb", "Domainb"); CredentialCache mycache = new credentialcache (); mycache.add (new uri ("http: // someserver / ws / servic e1.asmx ")," NTLM ", myCred); myWebService.Credentials = myCache; myWebService.ConnectionGroupName =" SameName "; result = myWebService.HelloWorld (); If you call a Web service by using the ASP.NET application original caller '

s identity, then you should avoid enabling UnsafeAuthenticatedConnectionSharing because connections would be shared across calls. Alternatively, you can enable UnsafeAuthenticatedConnectionSharing, and then assign users to individual connection groups by using a ConnectionGroupName. Either option is inefficient and results in a high number of pools. If you need to connect to separate discrete Web services, assign calls to separate pools using ConnectionGroupName. If you need to connect to a server that uses AuthPersistence (it authenticates a whole connection and not a single request), then you should set UnsafeAuthenticatedConnectionSharing to true , and specify some random connection group name. You will need to determine which application requests will go to that connection group because the server will no longer challenge for authentication on that connection.More Information

For more information about AuthPersistence, see Microsoft Knowledge Base article 318863, "HOW TO: Modify the AuthPersistence Metabase Entry Controls When Clients Are Authenticated," at http://support.microsoft.com/default.aspx?scid=kb;en- US; 318863. for more information on unsafeauthenticatedConnections, See the .net framework documentation.

Use preauthenticate with basic authentication

If you use Basic authentication, the proxy's PreAuthenticate property can be set to true or false. Set it to true to supply specific authentication credentials to cause a WWWauthenticate HTTP header to be passed with the Web request. This prevents the Web server from denying access to The Request and Performing Authentication on The Subsequent Retry Request.

Note ??? Pre-authentication only applies after the Web service successfully authenticates the first time. Pre-authentication has no impact on the first Web request.private void ConfigureProxy (WebClientProtocol proxy, string domain, string username, string password) {// To improve performance, force pre-authentication proxy.PreAuthenticate = true; // Set the credentials CredentialCache cache = new CredentialCache (); cache.Add (new Uri (proxy.Url), "Negotiate", new NetworkCredential (username, password, Domain); proxy.credentials = cache; proxy.connectiongroupname = usrname;

Threading

Web Services Use asp.net thread pooling to process requests. To Ensure That your Web Services Use the thread pool Most Effective, Consider the Following Guidelines:

Tune The Thread Pool Using The Formula For Red Bucing Contention. Consider Miniothreads and MinWorkerthreads for IntermitTent Burst loading.

Tune the thread pool by using the formula for reducing contention

The Formula for Reducing Contention can give you a good starting point for tuning the ASP.NET thread pool. Consider using the Microsoft product group recommended settings (shown in Table 10.2) if you have available CPU, your application performs I / O bound operations ( SUCH AS CALLING A Web Method OR Accessing The File System), AND You Have Queued Requests as Indicated by The ASP.NET Applications / Requests In Application Queue Performance Counter.

Table 10.2: Recommended Threading Settings for Reducing Contention

Configuration settingdefault (.NET 1.1) Recommended ValuemaxConnection212 * # cpusmaxiothreads20100maxworkerthreads20100Minfreethreads888 * # cpusminLocalRequestFreethreads476 * #cpus

To address this issue, you need to configure the following items in Machine.config. The changes described in the following list should be applied across the settings and not in isolation. For a detailed description of each of these settings, see "Thread Pool Attributes "in Chapter 17," Tuning .NET Application Performance. "

Set maxconnection to 12 * # of CPUs. This setting controls the maximum number of outgoing HTTP connections allowed by the client, which in this case is ASP.NET. The recommendation is to set this to 12 times the number of CPUs. Set maxIoThreads to 100. This setting controls the maximum number of I / 0 threads in the common language runtime (CLR) thread pool. This number is then automatically multiplied by the number of available CPUs. The recommendation is to set this to 100. Set maxWorkerThreads to 100 . This setting controls the maximum number of worker threads in the CLR thread pool. This number is then automatically multiplied by the number of available CPUs. The recommendation is to set this to 100. Set minFreeThreads to 88 * # of CPUs. The worker process uses this setting to queue up all the incoming requests if the number of available threads in the thread pool falls below the value for this setting. This setting effectively limits the number of concurrently executing requests to ma xWorkerThreads -.... minFreeThreads The recommendation is to set this to 88 times the number of CPUs This limits the number of concurrent requests to 12 (assuming maxWorkerThreads is 100) Set minLocalRequestFreeThreads to 76 * # of CPUs This worker process uses this setting to queue up requests from localhost (where a Web application calls a Web service on the same server) if the number of available threads in the thread pool falls below this number. This setting is similar to minFreeThreads, but it only applies to requests that use localhost ................ ..

Note ??? The above recommendations are starting points rather than strict rules You should perform appropriate testing to determine the correct settings for your environment.If the formula has worked, you should see improved throughput and less idle CPU time.:

CPU utilization should go up. Throughput should increase (ASP.NET Applications / Requests / Sec should go up), Requests in the application queue (ASP.NET Applications / Requests in Application Queue) should go down.

If this does not improve your performance, you may have a CPU-bound situation. If this is the case, by adding more threads you increase thread context switching. For more information, see "ASP.NET Tuning" in Chapter 17, "Tuning .NET Application Performance. "

More information

For more information, see Microsoft Knowledge Base article 821268, "PRB: Contention, Poor Performance, and Deadlocks When You Make Web Service Requests from ASP.NET Applications," at http://support.microsoft.com/default.aspx?scid = Kb; EN-US; 821268.

Consider Miniothreads and MinWorkerthreads for Intermit Burst Load

If you have burst load scenarios that are intermittent and short (0 to 10 minutes), then the thread pool may not have enough time to reach the optimal level of threads. The use of minIoThreads and minWorkerThreads allows you to configure a minimum number of worker AND I / O Threads for loading conditions.

At The Time of this Writing, You NEED A Supported Fix To Configure The Settings. For More Information, See The Following Microsoft Knowledge Base Articles:

810259, "FIX: SetMinThreads and GetMinThreads API Added to Common Language Runtime ThreadPool Class," at http://support.microsoft.com/default.aspx?scid=kb;en-us;810259 827419, "PRB:. Sudden Requirement For a larger number of threads from the threadpool class may result in Slow Computer response Time, "at http://support.microsoft.com/default.aspx?scid=kb;n-us;827419.more information

For More Information About Threading and Web Services, See:

"ASP.NET Tuning" in Chapter 17, "Tuning .NET Application Performance" Microsoft Knowledge Base article 821268, "PRB: Contention, Poor Performance, and Deadlocks When You Make Web Service Requests from ASP.NET Applications," at http: / /Support.Microsoft.com/default.aspx?scid=kb;n-us; 821268.

One-Way (Fire-And-Forget) Communication

Consider using the OneWay attribute if you do not require a response. Using the OneWay property of SoapDocumentMethod and SoapRpcMethod in the System.Web.Services.Protocols namespace frees the client immediately instead of forcing it to wait for a response.

For a Method To Support Fire-And-Forget Invocation, You Must Decorate It With The Oneway Attribute, AS Shown In The Following Code Snippet.

[SOAPDocumentMethod (Oneway = true)] [WebMethod (Description = "Returns Control Immediately)] Public void SomeMethod () {...}

................................................. ...Clakens.

Asynchronous Web Methods

You can call a Web service asynchronously regardless of whether or not the Web service has been implemented synchronously or asynchronously. Similarly, you can implement a synchronous or asynchronous Web service, but allow either style of caller. Client-side and server-side asynchronous processing is generally performed to free up the current worker thread to perform additional work in parallel.The asynchronous implementation of a Web method frees up the worker thread to handle other parallel tasks that can be performed by the Web method. This ensures optimal utilization of the thread Pool, Resulting in Throughput Gains.

For normal synchronous operations, the Web services asmx handler uses reflection on the assembly to find out which methods have the WebMethod attribute associated with them. The handler simply calls the appropriate method based on the value of the SOAP-Action HTTP header.

However, The Web Services Asmx Handler Treats Asynchronous Web Methods Differently. It looks for methods That Adhere to the Following Rules:

Methods Adhere to the Asynchronous Design Pattern:

There are BeginXXX and EndXXX methods for the actual XXX method that you need to expose. The BeginXXX method returns an IAsyncResult interface, takes whatever arguments the Web method needs, and also takes two additional parameters of type AsyncCallback and System.Object, respectively. The Endxxx Method Takes An IasyncResult As a Parameter And Returns The Return Type of Your Web Method. Both Methods Are Decorated with The WebMethod Attribute.

The Web Services Asmx Handler THEEN Exposed The Method, AS Shown In The Following Code Snippet.

[WebMethod] IAsyncResult BeginMyProc (...) [WebMethod] EndMyProc (...) // the WSDL will show the method as MyProc (...) The Web services asmx handler processes incoming requests for asynchronous methods as follows:

Call the BeginXXX method. Pass the reference to an internal callback function as a parameter to the BeginXXX method, along with the other in parameters. This frees up the worker thread processing the request, allowing it to handle other incoming requests. The asmx handler holds on to the HttpContext of the request until processing of the request is complete and a response has been sent to the client. Once the callback is called, call the EndXXX function to complete the processing of the method call and return the response as a SOAP response Release the httpcontext for the request.

Consider The Following Guidelines for Asynchronous WebMMETHODS:

Use asynchronous web methods for I / o Operations. Do Not Use asynchronous Web Methods When You Depend On Worker Threads.

Use asynchronous web methods for I / o Operations

Consider Using Asynchronous Web Methods if You Perform I / O-Bound Operations Such as:

Accessing Streams File I / O Operations Calling Another Web Service

The .NET Framework provides the necessary infrastructure to handle these operations asynchronously, and you can return an IAsyncResult interface from these types of operations. The .NET Framework exposes asynchronous methods for I / O-bound operations using the asynchronous design pattern. The libraries that Use this pattern has beginxx and endxxx methods.

The Following Code Snippet Shows The Implementation of Anotronous Web Method Calling Another Web Service.

// The client W / S public class AsynchWSToWS {WebServ asyncWs = null;? Public AsynchWSToWS () {asyncWs = new WebServ ();} [System.Web.Services.WebMethod] public IAsyncResult BeginSlowProcedure (int milliseconds, AsyncCallback cb, object s) {// make call to other web service and return the IAsyncResult return asyncWs.BeginLengthyCall (milliseconds, cb, s);} [System.Web.Services.WebMethod] public string EndSlowProcedure (IAsyncResult call) {return asyncWs.EndLengthyCall ( call);}} // The server W / S public class WebServ {[WebMethod] public string LengthyCall (int milliseconds) {Thread.Sleep (milliseconds); return "Hello World";}} Asynchronous implementation helps when you want to free up the worker thread instead of waiting on the results to return from a potentially long-running task. For this reason, you should avoid asynchronous implementation whenever your work is CPU bound because you do not have idle CPU to service more threads. In this case , An asynchronous implementation results in in in .

Note ??? You should not use asynchronous Web methods when accessing a database. ADO.NET does not provide asynchronous implementation for handling database calls. Wrapping the operation in a delegate is not an option either because you still block a worker thread.

You Should Only Consider Using An Asynchronous Web Method if You Are Wrapping An Asynchronous Operation That Hands Back AN

IASYNCRESULT REFERENCE.

Do Not Use asynchronous Web Methods When You Depend On Worker Threads

You should not implement Web methods when your asynchronous implementation depends upon callbacks or delegates because they use worker threads internally. Although the delegate frees the worker thread processing the request, it uses another worker thread from the process thread pool to execute the method. This is a thread that can be used for processing other incoming requests to the Web service. The result is that you consume a worker thread for the delegate-based operation and you increase context switching.Alternatively, you can use synchronous Web methods and decrease the minFreeThreads setting SO That The Worker Threads Can Take Requests and Execute Them Directly.

In this Scenario, You Could Block The Original Worker Thread by Implementing The Web Method To Run Synchronously - based Implementation IS Shown In The Following Code Snippet.

// delegate public delegate string LengthyProcedureAsyncStub (int milliseconds); // actual method which is exposed as a web service [WebMethod] public string LengthyCall (int milliseconds) {System.Threading.Thread.Sleep (milliseconds); return "Hello World" ;} [WebMethod] public IAsyncResult BeginLengthyCall (int milliseconds, AsyncCallback cb, object s) {LengthyProcedureAsyncStub stub = new LengthyProcedureAsyncStub (LengthyCall); // using delegate for asynchronous implementation return stub.BeginInvoke (milliseconds, cb, null);} [System .Web.services.webmethod] public string endlength {returnus (call);}

Asynchronous Invocation

Web Services Clients Can Call A Web Service Either Synchronously or Asynchronously, Independently of The Way The Web Service IS IMPLEMENTED.

For server applications, using asynchronous calls to a remote Web service is a good approach if the Web service client can either free the worker thread to handle other incoming requests or perform additional parallel work before blocking for the results. Generally, Windows Forms client applications call Web Services Asynchronously to Avoid Blocking The User Interface.note ??? The Http Protocol Allows At Most Two SimultaneOutbound Calls from One Client To One Web Service.

The WSDL-Generated Proxy Contains Support for Both Types of Invocation. The Proxy Supports the asynchronous call by Exposing Beginxxx and Endxxx Methods.

The Following Guidelines Help You Decide WHETHER OR NOT CALLING A Web Service Asynchronously Is ApproPriate:

CONSIER CALLING Web Services Asynchronously WORK. Use asynchronous invocation to call Multiple unrelated Web Services. Call Web Services Asynchronously For Ui Responsiveness.

Consider Calling Web Services Asynchronously When You Have Additional Parallel Work

Asynchronous invocation is the most useful when the client has additional work that it can perform while the Web method executes. Asynchronous calls to Web services result in performance and throughput gains because you free the executing worker thread to do parallel work before it is blocked by the Web Services Call and Waits for the Results. This Lets You Concurrently Process Any Work That Is Not Dendent on The Results of The Web Services Call. The Following Code Snippet Shows The approach.

private void Page_Load (object sender, System.EventArgs e) {serv = new localhost.WebService1 (); IAsyncResult result = serv.BeginLengthyProcedure (5000, null, null); // perform some additional processing here before blocking // wait for the Asynchronous Operation to Complete Result.asyncwaithandle.waitone (); string retstr = serv.endlengthyprocedure (result);} Use asynchronous invocation to call Multiple Unrelated Web Services

Consider asynchronous invocation if you need to call multiple Web services that do not depend on each other's results. Asynchronous invocation lets you call the services concurrently. This tends to reduce response time and improve throughput. The following code snippet shows the approach.

private void Page_Load (object sender, System.EventArgs e) {serv1 = new WebService1 (); serv2 = new WebService2 (); IAsyncResult result1 = serv1.BeginLengthyProcedure (1000, null, null); IAsyncResult result2 = serv2.BeginSlowProcedure (1000, null, null); // wait for the asynchronous operation to complete WaitHandle [] waitHandles = new WaitHandle [2]; waitHandles [0] = result1.AsyncWaitHandle; waitHandles [1] = result2.AsyncWaitHandle; WaitHandle.WaitAll (waitHandles); // Depending Upon the Scenario You can // choise rebringstr1 = serv1.endlength (result1); string retstr2 = serv2.endslowprocedure (result2);}

Call Web Services Asynchronously For Ui Responsiveness

By calling a Web service asynchronously from a Windows Forms application, you free the main user interface thread. You can also consider displaying a progress bar while the call progresses. This helps improve perceived performance.

However, you need to perform some additional work to resynchronize the results with the user interface thread because the Web service call is handled by a separate thread. You need to call the Invoke method for the control on which you need to display the results.More Information

For more information, see the MSDN article, "At Your Service: Performance Considerations for Making Web Service Calls from ASPX Pages," at http://msdn.microsoft.com/library/en-us/dnservice/html/service07222003.asp .

Timeouts

It is very common for an ASP.NET application to call a Web service. If your application's Web page times out before the call to the Web service times out, this causes an unmanaged resource leak and a ThreadAbortException. This is because I / O completion threads and sockets are used to service the calls. As a result of the exception, the socket connection to the Web service is not closed and can not be reused by other outbound requests to the Web service. The I / O thread continues to process the Web Service Response.

To Avoid these Issues, Set Timeouts Appropriately As Follows:

Set your proxy timeout appropriately. Set your ASP.NET timeout greater than your Web service timeout. Abort connections for ASP.NET pages that timeout before a Web services call completes. Consider the responseDeadlockInterval attribute.

Set your proxy timeout appropriately

When you call a Web service synchronously, set the Timeout property of the Web service proxy. The default value is 100 seconds. You can programmatically set the value before making the call, as shown in the following code snippet.

MyWebserve Obj = new mywebserv (); obj.timeout = 15000; // in milliseconds

For ASP.NET applications, the Timeout property value should always be less than the executionTimeout attribute of the httpRuntime element in Machine.config. The default value of executionTimeout is 90 seconds. This property determines the time ASP.NET continues to process the request before it returns a timed out error. The value of executionTimeout should be the proxy Timeout, plus processing time for the page, plus buffer time for queues.Consider reducing the Proxy Timeout value from its default of 100 seconds if you do not expect clients to wait for such a long time. you should do this even under high load conditions when the outbound requests to the Web service could be queued on the Web server. As a second step, reduce the executionTimeout also. you might need to increase the value if you Expect the synchronous call to take more time than the default value before completive large file, you may need to increable the attribute value. as a second Step, Intriese The ExecutionTimeout Attribute To An Appropriate Value.

Set your ASP.NET TIMEOUT GREATER THAN YOUR Web Service Timeout

The Web service timeout needs to be handled differently, depending upon whether you call the Web service synchronously or asynchronously. In either case, you should ensure that the timeouts are set to a value less than the executionTimeout attribute of the httpRuntime element in Machine.config The Following Approaches Describe The Options for Setting The Timeouts Approprise:

. Synchronous calls to a Web service Set the proxy Timeout to an appropriate value, as shown in the following code snippet MyWebServ obj = new MyWebServ ();. Obj.Timeout = 15000; // in milliseconds You can also set the value in the proxy class generated by the WSDL for the Web service You can set it in the class constructor, as shown in the following code snippet public MyWebServ () {??? this.Url = "http:.. // someMachine / mywebserv / myserv .asmx "; ??? this.timeout = 10000 ;? // 10 seconds} or you can set it at the method. public string length {this.timeout = 10000;? // 10 seconds object [] results = this.invoke ("lengthproc", new object [] {sleeptime}; return (string));?} Asynchronous calls to a web service. In this case, you should decide on the number of seconds you can wait for the Web service call to return the results. When using a WaitHandle, you can pass the number of milliseconds the executing thread is blocked on the Wa itHandle before it aborts the request to the Web service This is shown in the following code snippet MyWebServ obj = new MyWebServ ();.. IAsyncResult ar = obj.BeginFunCall (5,5, null, null); // wait for not more than 2 seconds ar.AsyncWaitHandle.WaitOne (2000, false); if (ar.IsCompleted!) // if the request is not completed {WebClientAsyncResult wcar = (WebClientAsyncResult) ar; wcar.Abort (); // abort the call to Web service} Else {// Continue Processing The Results from Web Service} ABORT Connections for ASP.NET PAGES TIMEOUT BEFORE A Web Services Call Completes

After you make the configuration changes described in the previous section, if your Web pages time out while Web services calls are in progress, you need to ensure that you abort the Web services calls. This ensures that the underlying connections for the Web services calls are destroyed.To abort a Web services call, you need a reference to the WebRequest object used to make the Web services call. you can obtain this by overriding the GetWebRequest method in your proxy class and assigning it to a private member of the class before returning The WebRequest. This Approach Is Shown In The Following Code Snippet.

Private WebRequest_Request; Protected Override WebRequest GetWebRequest (URI URI) {_Request = Base.GetWebRequest (URI); returnes_Request;}

The Web Service, you Should That Invokees The Web Service, You Should That Aborts the Request IF A ThreadAbortException is thRown.

[System.Web.Services.Protocols.SoapDocumentMethodAttribute (...)] public string GoToSleep (int sleepTime) {bool timeout = true; try {object [] results = this.Invoke ( "GoToSleep", new object [] {sleepTime}) Timeout = false; return (string));?} finally {if (timeout && _Request! = null) _Request.abort ();}}

Note ??? Modifying generated proxy code is not recommended because the changes are lost as soon as the proxy is regenerated. Instead, derive from the proxy class and implement new functionality in the subclass whenever possible.

Consider the ResponseDeadlockInterVal Attribute

When you make Web services calls from an ASP.NET application, if you are increasing the value of both the proxy timeout and the executionTimeout to greater than 180 seconds, consider changing the responseDeadlockInterval attribute for the processModel element in the Machine.config file. The default value of this attribute is 180 seconds. If there is no response for an executing request for 180 seconds, the ASP.NET worker process will recycle.You must reconsider your design if it warrants changing the attributes to a higher value.

WebMethods

You Add the WebMethod Attribute to Those Public Methods in Your Web Services .asmx File That You Want To Be Exposed To Remote Clients. Consider The Following Web Method Guidelines:

Prefer primitive parameter types. When you define your Web method, try to use primitive types for the parameters. Using primitive types means that you benefit from reduced serialization, in addition to automatic validation by the .NET Framework. Consider buffering. By default, the BufferResponse configuration setting is set to true, to ensure that the response is completely buffered before returning to the client. This default setting is good for small amounts of data. for large amounts of data, consider disabling buffering, as shown in the following code snippet . [WebMethod (BufferResponse = false)] public string GetTextFile () {// return large amount of data} to determine whether or not to enable or disable buffering for your application, measure performance with and without buffering. Consider caching responses. for applications That Deal with relatively static data, consider caching the responses to avoid accessing the database you can every the cacheduratio n attribute to specify the number of seconds the response should be cached in server memory, as shown in the following code snippet. [WebMethod (CacheDuration = 60)] public string GetSomeDetails () {// return large amount of data} Note that because caching consumes server memory, it might not be appropriate if your Web method returns large amounts of data or data that frequently changes Enable session state only for Web methods that need it. Session state is disabled by default. If your Web service needs to maintain state . You can set the enablesession attribute to True for a specific WebMMETHOD, AS Shown In The Following Code Snippet. [WebMethod (EnableSession =

true)] public string GetSomeDetails () {// return large amount of data} Note that clients must also maintain an HTTP cookie to identify the state between successive calls to the Web method. For more information, see "WebMethodAttribute.EnableSession Property" on MSDN At http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebserviceswebmethodattributenaBleasessionTopic.asp.Serialization

The amount of serialization that is required for your Web method requests and responses is a significant factor for overall Web services performance Serialization overhead affects network congestion, memory consumption, and processor utilization To help keep the serialization overhead to a minimum..:

Reduce Serialization with Xmlignore. Reduce Round Trips. Consider XML Compression.

Reduce Serialization with Xmlignore

To limit which fields of an object are serialized when you pass the object to or from a Web method and to reduce the amount of data sent over the wire, use the XmlIgnore attribute as shown in the following code snippet. The XmlSerializer class ignores any field Annotated with this attribute.

Note ??? unlike the formatters dermatters derived from the

IFORMATTER Interface,

XMLSerializer Serializes Only Public MEMBERS.

// This is the class that will be serialized public class MyClass {// The str1 value will be serialized public string str1;... / * This field will be ignored when serialized-- unless it's overridden * / [XmlIgnoreAttribute] public string Str2;

Reduce Round Trips

Reducing round trips to a Web service reduces the number of times that messages need to cross serialization boundaries This helps reduce the overall serialization cost incurred Design options that help to reduce round trips include the following:.. Use message-based interaction with a message- based programming model, rather than an RPC style that requires multiple object interactions to complete a single logical unit of work. in some cases, split a large payload into multiple calls to the Web service. Consider making the calls in parallel using asynchronous invocation instead of In Series. This Does Not Technical The Total Number of Round Trips, But in Essence The Client Waits for Only A Single Round Trip.

Consider XML Compression

Compressing the Xml Payload Sent over The Wire Helps Reduce The Network Traffic Signify. You Can IMPLEMENT XML Compression by Using One of the Following Techniques:

Use SOAP extensions on the server and client for the compression and decompression of requests and responses. Use a custom HTTP module on the server and override the proxy for the Web service on the client. Use HTTP compression features available in IIS 5.0 and later versions for .

More information

For More Information, See:

"XmlSerializer Architecture" in the November 2001 edition of MSDN magazine, at http://msdn.microsoft.com/msdnmag/issues/01/11/webserv/default.aspx Microsoft Knowledge Base article 314150, "INFO:. Roadmap for XML Serialization in the .net framework, "at http://support.microsoft.com/default.aspx?scid=kb;n-us; 314150. Microsoft Knowledge Base Article 313651," Info: RoadMap for XML in the .net framework , "at http://support.microsoft.com/default.aspx?scid=kb;n-us; 313651. Microsoft Knowledge Base Article 317463," How to: validate XML Fragments Against An XML Schema In Visual Basic .NET, "at http://support.microsoft.com/default.aspx?scid=kb;n-us; 317463.cching

Caching is a great way to improve Web services performance. By reducing the average request time and easing server load, caching also helps scalability. You can cache frequently used data applicable to all users, or you can cache SOAP response output. You can cache application . data by using ASP.NET caching features You can cache SOAP output by using either the ASP.NET output cache or by employing perimeter caching When designing a caching strategy for your Web services, consider the following guidelines.:

Consider Output Caching for Less Volatile Data. Consider PROVIDING CACHE-Related Information To Clients. Consider Perimester Caching.

Consider Output Caching for LESS VOLATILE DATA

If portions of your output are static or nearly static, use ASP.NET output caching. To use ASP.NET output caching with Web services, configure the CacheDuration property of the WebMethod attribute. The following code snippet shows the cache duration set to 30 seconds .

[WebMethod (Cacheduration = 30)] public string someMethod () {....

For more information, See Microsoft Knowledge Base Article 318299, "How to: Perform Output Caching with Web Services in Visual C # .NET," At http://support.microsoft.com/default.aspx?scid=kb;n-us ; 318299.conider providing cache-limited information to clients

Web services clients can implement custom caching solutions to cache the response from Web services. If you intend that clients of your Web services should cache responses, consider providing cache expiration-related information to the clients so that they send new requests to the Web service only After the the Web Service Respiration Time.

Consider perimeter caching

If the output from your Web services changes infrequently, use hardware or software to cache the response at the perimeter network. For example, consider ISA firewall-based caching. Perimeter caching means that a response is returned before the request even reaches the Web server, Which Reduces the Number of Requests That NEED TO BE Serviced.

For More Information About Isa Caching, See The White Paper, "Scale-Out Caching with ISA," at http://www.microsoft.com/isaser/techinfo/deployment/scaleoutcachingwithisa.asp.

State Management

Web services state can be specific to a user or to an application. Web services use ASP.NET session state to manage per-user data and application state to manage application-wide data. You access session state from a Web service in the same way you do from an ASP.NET application - by using the Session object or System.Web.HttpContext.Current you access application state using the Application object, and the System.Web.HttpApplicationState class provides the functionality.Maintaining session state has an impact. . on concurrency If you keep data in session state, Web services calls made by one client are serialized by the ASP.NET runtime Two concurrent requests from the same client are queued up on the same thread in the Web service -. the second request waits Until The First Request IS Processed. If You Do Not Use Session Data IN A Web Method, You Should Disable Sessions for That Method.

Maintaining state also affects scalability. First, keeping per-client state in-process or in a state service consumes memory and limits the number of clients your Web service can serve. Second, maintaining in-process state limits your options because in-process state Is Not Shared by Servers In A Web Farm.

If your Web service needs to maintain state between client requests, you need to choose a design strategy that offers optimum performance and at the same time does not adversely affect the ability of your Web service to scale. The following guidelines help you to ensure efficient state Management:

Use session state only where it is needed. Avoid Server Affinity.

Use session state only where it is needed

To maintain state between requests, you can use session state in your Web services by setting the EnableSession property of the WebMethod attribute to true, as shown in the following code snippet. By default, session state is disabled. [WebMethod (EnableSession = true) ] YourWebMethod () {...}

Since You Can Enable session State At The Web Method Level, Apply this Attribute Only to Those Web Methods That NEED IT.

Note ??? Enabling session state pins each session to one thread (to protect session data). Concurrent calls from the same session are serialized once they reach the server, so they have to wait for each other, regardless of the number of CPUs.

Avoid Server Affinity

If you do use session state, in-process session state offers the best performance, but it prevents you from scaling out your solution and operating your Web services in a Web farm. If you need to scale out your Web services, use a remote session Store That Can Be Accessed by All Web Servers In The Farm.

Bulk Data Transfer

You Have The Following Basic Options for Passing Large Amounts of Data Including Binary Data To and from Web Methods:

Using a Byte Array Web Method Parameter. Returning a Url from The Web Service. Using Streaming.

Using a byte array Web method parameter

... With this approach, you pass a byte array as a method parameter An additional parameter typically specifies the transfer data size This is the easiest approach, and it supports cross-platform interoperability However, it has the following issues:

If a failure occurs midway through the transfer, you need to start again from the beginning. If the client passes an arbitrary amount of data that exceeds your design limitations, you run the risk of running out of memory or exceeding your CPU thresholds on the server Your Web Service Is Also Susceptible To Denial of Service Attacks.note That You Can Limit The Maximum Soap Message Size for A Web Service By Using The maxrequestlength setting in

Section of the Web.config File. In The Following Example, The Limit IS set to 8 kb.

Base 64 encoding

For binary data transfert, you can use 64 to encode the data. Base 64 encoding is suitable for cross-platform interoperability if your Web Service Has A HETEROGENEOUS Client Audience.

This approach is more suitable if your data is not large and the encoding / decoding overhead and size of the payload are not of significant concern. For large-sized data, you can implement a WSE filter and various compression tools to compress the message before Sending it over the wire.

For More Information About Base 64 Encoding and Decoding, See:

Microsoft Knowledge Base article 191239, "Sample Base 64 Encoding and Decoding," at http://support.microsoft.com/default.aspx?scid=kb;EN-US;q191239. MSDN article, "Web Methods Make It Easy to Publish Your App's Interface Over the Internet "by Paula Paul, at http://msdn.microsoft.com/msdnmag/issues/02/03/webmethods/.

Returning a Url from the Web Service

Returning a Url from the Web Service is The Preferred Option for Large File Downloads. With this approach, you Return a Url to the client, and the client the file.

You can consider using the Background Intelligent Transfer Service (BITS), a Windows service, for this purpose. For more information about BITS, see the MSDN article, "Write Auto-Updating Apps with .NET and the Background Intelligent Transfer Service API" by Jason Clark, at http://msdn.microsoft.com/msdnmag/issues/03/02/bits/.if you need to use book for your .net application for Uploading and Downloading of Files, You Can Use the Updater Application Block For more information, see the msdn article "Updater Application Block for .NET" at http://msdn.microsoft.com/library/en-us/dnbda/html/Updater.asp.

Although returning a URL works for downloads, it is of limited use for uploads. For uploads, you must call the Web service from an HTTP server on the Internet, or the Web service will be unable to resolve the supplied URL.

Using streaming

If you need to transfer large amounts of data (several megabytes, for example) from a Web method, consider using streaming. If you use streaming, you do not need to buffer all of the data in memory on the client or server. In addition Streaming allows you to send progress Updates from a long-running Web Service Operation to aclient That Blocked Waiting for the Operation To Return.

In most cases, data is buffered on both the server and client. On the server side, serialization begins after the Web method has returned, which means that all of the data is usually buffered in the return value object. On the client side, the Deserialization of the entire response occurs before buffering data in membrate, again buffering data.

You Can Stream Data from A Web Service In Two Ways:

Implementing IList Implementing IXMLSerializable

Implementing ilist

The XmlSerializer has special support for types that implement IList whereby it obtains and serializes one list item at a time. To benefit from this streaming behavior, you can implement IList on your return type, and stream out the data one list item at a time without First buffering it.

While this Approach Provides Streaming and the Ability To Send Progress Updates, It forces You To Work with Types That Implement Ilist and To Break Data Down Into List Items.

Note ??? The Serializer is Still Responsible for Serializing and Deserialiance Each Individual List item.

Another disadvantage is that the progress is reported from the returned type. This means that the instance of your type that implements IList used on the client side must be able to communicate progress to the client application while the serializer calls IList.Add. While this is Achievable, IT is not ideal.

The Following Server and Client Code Samples Show How To Implement this Approach. The Server Code Is Shown Below.

// Server code // If the server wants to return progress information, the IList indexer would // have to know the current progress and return a value indicating that progress // when it is called public class MyList:.? IList {int progress = 0;? Public object this [int index] ?? {??????? {? ???? // pretend to do something what takes .5 seconds ????? system.threading.thread.sleep (500); ????? IF (Progress <= 90) ?????? Return Progress = 10; ????? Else ??????? Return "Some Data Goes HERE";? ??} ????? {????? // Todo :? add setter implemementation ???}?} ?? ... other members omitted} [WebMethod] public mylist dolongoperty () {? / / to prevent ASP.NET from buffering the response, the WebMethod must set // the BufferOutput property to false HttpContext.Current.Response.BufferOutput = false;??? return new MyList ();} By using the above code, the Web Service Response is Streamed Out and Consists of 10 Progress Data Points (10 to 100), Followed by a string of data.

The corresponding method on the client proxy class must return a type that implements IList. This type must know how to stream items as they are added to the list, and, if required, how to report progress information as it is retrieved from the stream. The Relevant Mylist Member on The Client Is The Add Method:

Note ??? with .NET Framework 1.1 You Must Manually Edit The Generated Proxy Code Because Wsdl.exe and The

Add Web Reference Option In Visual Studio.Net Generate A Proxy Class with a Method That Returns An Object Array. You NEED TO MODIFY THIS TO RETURN A TYPE THAT IMPLEMENTS

ILIST.

The Client Code Is Shown Below.

// Client Code Public Class MyList: ilist {? Public int add (object value)? {? ?? f (progress <100) ??? {?? ??? progress = convert.toint32 (value);??? ?? console.writeline ("Progress IS {0}", Progress; ???} ??? Else ??? {? ????????????????????? {? ???? ?? ???} ?? return 0;}} The client's proxy class then contains a method that returns MyList as shown below: public ProgressTestClient.MyList DoLongOperation () {... code omitted} Implementing IXmlSerializable?

Another possible approach is to create a type that implements IXmlSerializable, and return an instance of this type from your Web method and the client proxy's method. This gives you full control of the streaming process. On the server side, the IXmlSerializable type uses the WriteXml Method to Stream Data Out:

This solution is slightly cleaner than the previous approach because it removes the arbitrary restriction that the returned type must implement IList. However, the programming model is still a bit awkward because progress must be reported from the returned type. Once again, in.NET Framework 1.1, You Must Also Modify The Generated Proxy Code To Set The Correct Method Return Type.

The Following Code Sample Shows How To Implement this approach.

Public Class ProgressTest: ixmlserializable {? public void writexml (system.xml.xmlwriter write)? {?? int propress = 0; ??? while (progress <= 100) ??? {?? ??? Writer.writeElementstring ("Progress", ??????????????????????? "http://progresstest.com", Progress.Tostring ( ))); ????? Writer.flush (); ????? progress = 10; ???? // pretend to do something what takes 0.5 second ????? system.threading.thread. Sleep (500); ???} ??? Writer.writeElementString ("THEDATA", ??????????????????????????????????????????????????????????????????????????????????????????????????????????? "http://progresstest.com", "Some Data Goes Here");?}} The Web Method Must Disable Response BuffressTest Type, AS Shown Below.

[WebMethod] Public ProgressTest DOLONGOPERATION2 () {? HttpContext.current.response.bufferoutput = false;? Return new progressTest ();

More information

For More Information About Bulk Data Transfer, See:

Microsoft Knowledge Base Article 318425, "HOW TO: Send and received binary documents by using an asp.net Web service and visual c # .net," at http://support.microsoft.com/default.aspx?scid=kb;n -US; 318425. MSDN Article, "Large Data Strategies," at http://msdn.microsoft.com/library/en-us/dnservice/html/service11072001.asp. msdn article, "XML, SOAP, AND BINARY DATA "At http://msdn.microsoft.com/library/en-us/dnwebsrv/html/infoseet_whitepaper.asp.

Attachments

You Have Various Options When Handling Attachments with Web Services. When Choosing Your Option, Consider The Following:

WS-Attachments. WSE versions 1.0 and 2.0 provide support for WS-Attachments, which uses Direct Internet Message Encapsulation (DIME) as an encoding format. Although DIME is a supported part of WSE, Microsoft is not investing in this approach long term. DIME is limited because the attachments are outside the SOAP envelope. Base 64 encoding. use Base 64 encoding. At this time, you should use Base 64 encoding rather than WS-Attachments when you have advanced Web service requirements, such as security. Base 64 encoding results in a larger message payload (up to two times that of WS-Attachments). you can implement a WSE filter to compress the message with tools such as GZIP before sending it over the network for large amounts of binary data. If you can not afford the message size that Base 64 introduces and you can rely on the transport for security (for example, you rely on SSL or IPSec), then consider the WSE WS-Attachments implementation. Securing the message is preferable to securing the transport so that messages can be routed securely, whereas transport only addresses point-to-point communication. SOAP Message Transmission Optimization Mechanism (MTOM). MTOM, which is a derivative work of SOAP messages with attachments (SwA), is likely to be the FUTURE Interop Technology. MTOM is Being Standardized by The World Wide Web Consortium (W3C) And is much more composition-friendly Than Swa.soap Messages with attachments (SWA)

SwA (also known as WS-I Attachments Profile 1.0) is not supported. This is because you can not model a MIME message as an XML Infoset, which introduces a non-SOAP processing model and makes it difficult to compose SwA with the rest of the WS- * Protocols, Including WS-Security. The W3C MTOM WORK WAS SPECIFICLY CHARTERED TO FIX THIS PROBLICLY SWA, AND Microsoft Is Planning to Support MTOM in WSE 3.0.com Interop

Calling single-threaded apartment (STA) objects from Web services is neither tested nor supported. The ASPCOMPAT attribute that you would normally use in ASP.NET pages when calling Apartment threaded objects is not supported in Web services.

More information

For more information, See Microsoft Knowledge Base Article 303375, "Info: XML Web Services and Apartment Objects," At http://support.microsoft.com/default.aspx?scid=kb;n-us; 303375.

Measuring and analyzing web services performance

The quickest way to measure the performance of a Web services call is to use the Microsoft Win32? QueryPerformanceCounter API, which can be used with QueryPerformanceFrequency to determine the precise number of seconds that the call consumed.

Note ??? you can also use the

ASP.NET/Request Execution Time Performance Counter on The Server Hosting The Web Service.

More information

For more information, see. "How To: Time Managed Code Using QueryPerformanceCounter and QueryPerformanceFrequency" in the "How To" section of this guide For more information about measuring Web services performance, see "Web Services" in Chapter 15, "Measuring .NET Application Performance "

Web service enhancements

Web Service Enhancements (WSE) is an implementation provided to support emerging Web services standards. This section briefly explains WSE, its role in Web services, and sources of additional information.WSE 2.0 provides a set of classes implemented in the Microsoft.Web.Services .dll to support The Following Web Services Standards:

WS-Security WS-Secureconversation WS-Trust WS-Policy WS-Addressing WS-Referrals WS-Attachments

Figure 10.2 Shows How WSE Extends The .NET Framework To Provide this functionality.

Figure 10.2: WSE Runtime

The WSE runtime consists of a pipeline of filters that intercepts inbound SOAP requests and outgoing SOAP response messages. WSE provides a programming model to manage the SOAP headers and messages using the SoapContext class. This gives you the ability to implement various specifications that it supports.

More information

For More Information About WSE, See the MSDN Article, "Web Services Enhancements (WSE)," At http://msdn.microsoft.com/webservices/building/wse/default.aspx.

Summary

Web services are the recommended communication mechanism for distributed .NET applications. It is likely that large portions of your application are depending on them or will depend on them. For this reason, it is essential that you spend time optimizing Web services performance and that you Design and Implement your Web Services with knowledge of the important factory trata confect Their Performance and scalability.

This chapter has presented the primary Web services performance and scalability issues that you must address. It has also provided a series of implementation techniques that enable you to tackle these issues and build highly efficient Web services solutions.

Additional Resources

For more information, see the following resources: For a printable checklist, see "Checklist: Web Services Performance". In the "Checklists" section of this guide Chapter 4, "Architecture and Design Review of a .NET Application for Performance and Scalability" Chapter 13, "Code Review: .NET Application Performance" See "Web Services" and "ASP.NET" Sections. Chapter 15, "Measuring .NET Application Performance" See The "Web Services" and "ASP.NET" section. Chapter 16, "Testing .NET Application Performance" Chapter 17, "Tuning .NET Application Performance" See the "Web Services Tuning" and "ASP.NET Tuning" sections. For key recommendations to help you create high-performance .NET Enterprise Services Components, See ".Net Enterprise Services Performance" by Richard Turner, on msdn at http://msdn.microsoft.com/library/en-us/dncomser/html/entsvcperf.asp. for more information on using Microsoft WSE, SEE Microsoft Knowledge Base Article 821377, "Support Webcast: Introduction TO Microsoft W EB Services enhancements, "at http://support.microsoft.com/default.aspx?scid=kb;n-us; 821377.

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

New Post(0)