Web Services (ASMX 1.1) Performance Guidelines - WebMethods

From Guidance Share

Jump to: navigation, search

- J.D. Meier, Srinath Vasireddy, Ashish Babbar, and Alex Mackman


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 for every client request. You can use the CacheDuration 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, then you can set the EnableSession attribute to true for a specific Web method, 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.

References

For more information, see "WebMethodAttribute.EnableSession Property" on MSDN at http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebServicesWebMethodAttributeClassEnableSessionTopic.asp.

Personal tools