The GZipMessageEncoder is a great sample for learning about MessageEncoders in general. In this post, I will expand the GZipMessageEncoder to do both GZip and Deflate compression. This will accomplish two things: (1) explain various pieces of a MessageEncoder and (2) set a groundwork for future posts.
Since the MessageEncoder will now support both GZip and Deflate, it would be a good idea to change the name. To do this I did a replace in all files from "GZipMessageEncod" to "MyCompressionMessageEncod":
I did the same thing with "GZipEncoder" to "CompressionEncoder" to change the namespace. I also changed the assembly name in the solution explorer and in the project properties. Then I changed the filenames in the project. You can try to repeat all these steps or just download the finished code.
The first thing that is needed is a switch to flip between GZip and Deflate. Since my plan is to enable more than just GZip and Deflate in later posts, this should be an enum.
namespace Microsoft.Samples.CompressionEncoder
{
public enum CompressionAlgorithm
{
GZip,
Deflate,
}
}
In order to enable this switch there are a lot of places that have to change. The first thing I'll change is the binding element. This is what is used by WCF to configure the binding.
//This is the binding element that, when plugged into a custom binding, will enable the GZip encoder
public sealed class MyCompressionMessageEncodingBindingElement
: MessageEncodingBindingElement //BindingElement
, IPolicyExportExtension
{
//We will use an inner binding element to store information required for the inner encoder
MessageEncodingBindingElement innerBindingElement;
CompressionAlgorithm compressionAlgorithm;
//By default, use the default text encoder as the inner encoder
public MyCompressionMessageEncodingBindingElement()
: this(new TextMessageEncodingBindingElement(), CompressionAlgorithm.GZip) { }
public MyCompressionMessageEncodingBindingElement(
MessageEncodingBindingElement messageEncoderBindingElement,
CompressionAlgorithm compressionAlgorithm)
{
this.innerBindingElement = messageEncoderBindingElement;
this.compressionAlgorithm = compressionAlgorithm;
}
public MessageEncodingBindingElement InnerMessageEncodingBindingElement
{
get { return innerBindingElement; }
set { innerBindingElement = value; }
}
Read more: Dustin Metzgar's blog
0 comments:
Post a Comment