Introduction
WCF is a programming platform that allows us to build, configure, and deploy the network distributed services. WCF works on a Service-oriented concept. Some points can help us improve the performance of WCF.
1. Select the proper WCF Binding
The selection of the WCF binding also affects the performance. There are many types of bindings 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: https://www.c-sharpcorner.com/UploadFile/87b416/type-of-bindings-that-wcf-supports/
2. Throttling
The throttling of services is another key element for WCF performance tuning. WCF throttling provides the prosperities maxConcurrentCalls, maxConcurrentInstances, and maxConcurrentSessions, which can help us limit the number of instances or sessions 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 configuration -->
</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 allows 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>
<!-- Other service model configuration -->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<!-- Other service model configuration -->
</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 the sticky session. We can also use Windows Server AppFabric as the cache server.
5.SQL Server Best Practices
Follow the best practice for the SQL Server if your WCF service has database operations.
6.Close Proxy Connections
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 required by the end user. In other words, try to avoid sending unneeded data across the network.
8.Configure Transport Properties
The WCF transport properties 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 to prevent a single connection from exhausting the system resources and denying service to all other connections. Collection size limits help us restrict the consumption of resources.
Also, refer to Transport quotas for WCF on MSDN
9.ReaderQuotas Property
The ReaderQuotas property (like MaxDepth, MaxStringContentLength, MaxArrayLength, MaxBytesPerRead, and MaxNameTableCharCount) can assist us in restricting message complexity to protect from DOS (Denial of Service) attacks.