What's new in WCF 4.5 - Part 6
Compress the Binary Encoder message
Begging from WCF 4.5 onwards, WCF binary message encoder supports message compression. Type of compression can be mention in the binding settings
Points to remember:
- Both the client and the service must configure the CompressionFormat property, if service is configured with compression and client is not configured then system will throw exception.
- Compression will work for HTTP, HTTPS and TCP protocol
- You need to create the custom binding to use this feature
- Compression is mostly useful if network bandwidth is a bottleneck. In the case where the CPU is the bottleneck, compression will decrease throughput.
<binaryMessageEncoding compressionFormat ="GZip"/>
In some client application ChannelFactory is used to create a communication between Client and Service. Creating ChannelFactor will increase the overhead by following operation.
- Constructing the ContractDescription tree
- Reflecting all of the required CLR types
- Constructing the channel stack
- Disposing of resources
In order to avoid this overhead, WCF 4.5 introduced the caching mechanism for ChannelFactor.
Caching will take three types of values
- CacheSetting.AlwaysOn - All instances of ClientBase within the app-domain can participate in caching
- CacheSetting.AlwaysOff - Caching is turned off for all instances of ClientBase
- CacheSetting.Default - Only instances of ClientBase created from endpoints defined in configuration files participate in caching within the app-domain
ClientBase < IService>.CacheSetting = CacheSetting.AlwaysOn;
foreach (string id in lstEmpIds)
using (TestClient proxy = new TestClient(new BasicHttpBinding(),
- Always create the service with Interface->Implementation format, mention the contract in Interface.
- Define the service in Class library and refer the class library in Host project. Don’t use service class in host project.
- Change the instance mode to per call as default.
- Always catch exception using try/catch block and throw exception using FaultException < T >.
- Logging and Include exception should be enable while compiling the project in debug mode. While in production deployment disable the logging and Include exception details.