WCF Performance Tuning

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.

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

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.

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.