WCF Performance Tuning

Introduction

WCF is a programming platform that allows us to build, configure and deploy the network distributed services. Basically WCF works on a Service oriented concept. There are some points that can help us to improve the performance of WCF.

1. Select proper WCF Binding

Selection of the WCF binding also affects the performance. There are many types of bindings are available in WCF Services. Each binding has a special purpose and security model. Depending on requirements we can select the proper binding type. For example if we create a WCF service that initially uses a WSHttpBinding. This binding has an extra cost for security, reliable sessions and transaction flow. If we select BasicHttpBinding instead of this then the performance is dramatically improved.

To learn more about binding in WCF, refer to the following link: http://msdn.microsoft.com/en-us/magazine/cc163394.aspx 

2. Throttling

The throttling of services is another key element for WCF performance tuning. WCF throttling provides the prosperities maxConcurrentCalls, maxConcurrentInstances, and maxConcurrentSessions, that can help us to limit the number of instances or sessions are created at the application level.

Attribute / Property

Description

maxConcurrentCalls

This specifies the maximum number of messages processed across the service host. The default value for this property is 16 (WCF 4.0 is improved to default is 16 * Processor Count).

maxConcurrentInstances

This specifies the maximum number of instances of a context object that executes at one time with the service. The default is Int32.MaxValue.

maxConcurrentSessions

This specifies the maximum number of sessions at one time within the service host object. The default value is 10 (WCF 4.0 increases that to 100 * Processor Count).


<configuration> 
  <system.serviceModel>
    <services>
      <service
        ....
               ....
               ....
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior  name="ServiceBehavior">
          <serviceThrottling
                              maxConcurrentCalls="16" maxConcurrentSessions="100" maxConcurrentInstances="10" />
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

3. Use data contract serialization

Serialization is the process of converting an object to a transferable format. XML serialization and binary are very useful when transferring objects over the network. XML serialization is very popular for its interoperability and binary serialization is used when transferring objects between two .NET applications.

Data contract serialization is about 10% faster than XML serialization. This can be significant if anyone is working with a large amount of data. The Data Contract Serializer can serialize public members as well as private and protected members.

4. Caching

External Dependency is another main problem with WCF Service Performance. We can use Caching to avoid this problem. Caching is allowing us to store data in memory or some other place from which we can retrieve it quickly. We have two options for caching, in-memory and external caching.

In-memory Caching

WCF services do not have access to the ASP.NET cache by default. We can do this using ASP.NET compatibility by adding the "AspNetCompatibilityRequirements" Attribute.

[AspNetCompatibilityRequirements ( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

<system.serviceModel>
....
<serviceHostingEnvironment  aspNetCompatibilityEnabled="true" />
....
</system.serviceModel>

External Caching

The problem with in-memory caching is that it is very difficult to expire an item from the cache when the user performs a change on it. The sticky session can help us to resolve the problem. All requests from the same source IP address are routed to the same server; this is called sticky session. We can also use Windows Server AppFabric as the cache server.

5. Follow the best practice for the SQL Server if your WCF service has database operations.

6. Always close the proxy connection when the client is done with it. When we close the proxy connection, the service's session expires and the connection is closed between the server and client.

7. Compress data: only serialize the data to be sent across the network and that is actually required by the end user. In other words try to avoid sending unneeded data across the network.

8. The WCF transport property like timeout, Memory allocation limits and Collection size limits also help to improve the performance of the service. The Timeout is used to mitigate DOS (Denial of Service) attacks. Memory allocation allows us prevent a single connection from exhausting the system resources and denying service to all other connections. Collection size limits help us to restrict the consumption of resources.

Also refer Transport quotas for WCF on MSDN: http://msdn.microsoft.com/en-us/library/ms731078.aspx

9. The ReaderQuotas property (like MaxDepth, MaxStringContentLength, MaxArrayLength, MaxBytesPerRead and MaxNameTableCharCount) can assist us in restricting message complexity to provide protection from DOS (Denial of Service) attacks.

http://msdn.microsoft.com/en-us/library/ms731325.aspx