Thursday, July 31, 2014

Using Rashan’s WCF Compression Library

In one of my previous posts, I have discussed the general idea of compressing the traffic of a WCF service and how I started implementing a reusable component to achieve this. In this post, let us see how easy to enable compression in any WCF service by using my reusable library, without changing any code of an existing implementation!

Installing the component

The compression library should be installed on both the WCF service hosting project and the client project, that consumes the service. This can be done easily by using Nuget.
  • If the Nuget package console is used, then execute the following command for each project (i.e. service and Clients).
PM> Install-Package Rashans.WCF.Compression
  • If the Nuget GUI is used, then search for “wcf compression encoder” and select the package with Id Rashans.WCF.Compression.
image

Configuring the WCF Service hosting project

1. The configuration details related to the compression library are defined in a custom binding element. So the first thing you have to do is to register the custom binding element as bellow;

  
    
 
    
  

2. Define the customBinding based on the inner encoder type. In the following example, I am defining 2 custom bindings; one is based on textMessageEncoder over HTTP transport and the other based on binaryMessageEncoding over TCP. Also note that I have set the compression type as “gzip”. Currently, the only compression method supports is gzip, but soon I will add the support for other compression types.

  
    
    
      
3. The remaining service definitions can be configured as required.

Configuring the WCF Service consuming projects

1. Just like in the service project, we need to register the custom binding element and define the custom bindings. That is, repeat the 1 and 2 operations in each client project that accessing the WCF service.
2. Register the custom metadata\policy importer.

  
    
       
    
  
  

A sample application with both server and client components can be found here;




Technorati Tags: ,,,

2 comments: