<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>WCF</title>
        <link>http://geekswithblogs.net/argot/category/10807.aspx</link>
        <description>WCF</description>
        <language>en-AU</language>
        <copyright>argot</copyright>
        <managingEditor>bruceworld@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>WCF Message Compression - Gzip, 7zip</title>
            <link>http://geekswithblogs.net/argot/archive/2009/10/27/wcf-message-compression---gzip-7zip.aspx</link>
            <description>&lt;p&gt;I was using &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/cc138373.aspx"&gt;Gzip Encoder&lt;/a&gt; to compress wcf message, it surprised me that sometimes the compression message size is even bigger than the original size, so I looked the code, I found within &lt;font face=""&gt;GZipMessageEncoderFactory.cs, the method "&lt;font face=""&gt;CompressBuffer" in the &lt;font face=""&gt;GZipMessageEncoderFactory class is not quite right. it was like this originally:&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;private static ArraySegment&amp;lt;byte&amp;gt; CompressBuffer(ArraySegment&amp;lt;byte&amp;gt; buffer, BufferManager bufferManager, int messageOffset)&lt;br /&gt;
{&lt;br /&gt;
   ....&lt;br /&gt;
  var byteArray = new ArraySegment&amp;lt;byte&amp;gt;(bufferedBytes, messageOffset,&lt;br /&gt;
                                                       &lt;strong&gt;bufferedBytes.Length - messageOffset&lt;/strong&gt;);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;  return byteArray;&lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;if we use the method above, the count of the new ArraySegment&amp;lt;T&amp;gt; will be the bufferedBytes, as &lt;font face=""&gt;BufferManager using &lt;font face=""&gt;TakeBuffer method to grab a buffer which is the nearest bigger 2&lt;sup&gt;N&lt;/sup&gt; byte, e.g. if input param is 522 bytes for &lt;font face=""&gt;TakeBuffer method, it will grab a buffer which has 1024 bytes. And the constructor of ArraySegment will take the third parameter(bold above) to reflects the actual size of the ArraySegment, if size of the &lt;font face=""&gt;bufferedBytes(first params) is less than third parameter, it will fill byte0 to the rest. that is why sometimes the size is even bigger than the original size. &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;it should be like this:&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;private static ArraySegment&amp;lt;byte&amp;gt; CompressBuffer(ArraySegment&amp;lt;byte&amp;gt; buffer, BufferManager bufferManager, int messageOffset)&lt;br /&gt;
{&lt;br /&gt;
   ....&lt;br /&gt;
  var byteArray = new ArraySegment&amp;lt;byte&amp;gt;(bufferedBytes, messageOffset,&lt;br /&gt;
                       &lt;strong&gt;compressedBytes.Length&lt;/strong&gt;);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;  return byteArray;&lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Also I found the Gzip compression doesn't have high compression ratio, I decided to use 7zip and gzip combination then, I found a good article about 7zip: &lt;a href="http://www.eggheadcafe.com/tutorials/aspnet/064b41e4-60bc-4d35-9136-368603bcc27a/7zip-lzma-inmemory-com.aspx"&gt;7Zip (LZMA) In-Memory Compression with C#&lt;/a&gt;, after adding it to my project, I can use it straight forward:&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;private static ArraySegment&amp;lt;byte&amp;gt; CompressBuffer(ArraySegment&amp;lt;byte&amp;gt; buffer, BufferManager bufferManager, int messageOffset)&lt;br /&gt;
{&lt;br /&gt;
 using (var memoryStream = new MemoryStream())&lt;br /&gt;
 {&lt;br /&gt;
  var zipperStream = new GZipStream(memoryStream, CompressionMode.Compress, true);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;  using (zipperStream)&lt;br /&gt;
   zipperStream.Write(buffer.Array,&lt;br /&gt;
      buffer.Offset, buffer.Count);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;  byte[] compressedBytes1 = memoryStream.ToArray();&lt;br /&gt;
  byte[] compressedBytes = SevenZip.Compression.LZMA.SevenZipHelper.Compress(compressedBytes1);&lt;br /&gt;
  byte[] bufferedBytes = bufferManager.TakeBuffer(&lt;br /&gt;
     compressedBytes.Length + messageOffset);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;  Array.Copy(compressedBytes, 0, bufferedBytes, messageOffset,&lt;br /&gt;
     compressedBytes.Length);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;  bufferManager.ReturnBuffer(buffer.Array);&lt;br /&gt;
  var byteArray = new ArraySegment&amp;lt;byte&amp;gt;(bufferedBytes, messageOffset,&lt;br /&gt;
     compressedBytes.Length);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;  return byteArray;&lt;br /&gt;
 }&lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;//Helper method to decompress an array of bytes&lt;br /&gt;
private static ArraySegment&amp;lt;byte&amp;gt; DecompressBuffer(ArraySegment&amp;lt;byte&amp;gt; buffer, BufferManager bufferManager)&lt;br /&gt;
{&lt;br /&gt;
 var memoryStream1 = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count - buffer.Offset);&lt;br /&gt;
 var memoryStream=new MemoryStream(SevenZip.Compression.LZMA.SevenZipHelper.Decompress(memoryStream1.ToArray()));&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt; var decompressedStream = new MemoryStream();&lt;br /&gt;
 int totalRead = 0;&lt;br /&gt;
 int blockSize = 1024;&lt;br /&gt;
 byte[] tempBuffer = bufferManager.TakeBuffer(blockSize);&lt;br /&gt;
 using (var gzStream = new GZipStream(memoryStream, CompressionMode.Decompress))&lt;br /&gt;
 {&lt;br /&gt;
  while (true)&lt;br /&gt;
  {&lt;br /&gt;
   int bytesRead = gzStream.Read(tempBuffer, 0, blockSize);&lt;br /&gt;
   if (bytesRead == 0)&lt;br /&gt;
    break;&lt;br /&gt;
   decompressedStream.Write(tempBuffer, 0, bytesRead);&lt;br /&gt;
   totalRead += bytesRead;&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 bufferManager.ReturnBuffer(tempBuffer);&lt;br /&gt;
   &lt;br /&gt;
 byte[] decompressedBytes = decompressedStream.ToArray();&lt;br /&gt;
 byte[] bufferManagerBuffer = bufferManager.TakeBuffer(decompressedBytes.Length + buffer.Offset);&lt;br /&gt;
 Array.Copy(buffer.Array, 0, bufferManagerBuffer, 0, buffer.Offset);&lt;br /&gt;
 Array.Copy(decompressedBytes, 0, bufferManagerBuffer, buffer.Offset, decompressedBytes.Length);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt; var byteArray = new ArraySegment&amp;lt;byte&amp;gt;(bufferManagerBuffer, buffer.Offset, decompressedBytes.Length);&lt;br /&gt;
 bufferManager.ReturnBuffer(buffer.Array);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt; return byteArray;&lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt; &lt;img src="http://geekswithblogs.net/argot/aggbug/135731.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>argot</dc:creator>
            <guid>http://geekswithblogs.net/argot/archive/2009/10/27/wcf-message-compression---gzip-7zip.aspx</guid>
            <pubDate>Tue, 27 Oct 2009 05:20:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/argot/comments/135731.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/argot/archive/2009/10/27/wcf-message-compression---gzip-7zip.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/argot/comments/commentRss/135731.aspx</wfw:commentRss>
        </item>
        <item>
            <title>WCF ReliableSession Retry Interval</title>
            <link>http://geekswithblogs.net/argot/archive/2009/10/27/wcf-reliablesession-retry-interval.aspx</link>
            <description>&lt;p&gt;Been using &lt;span class="identifier"&gt;ReliableSessionBindingElement within &lt;/span&gt;WCF, I found we can not set the retry interval, as we only can set MaxRetryCount. WCF uses an internal algorithm to determine when to retransmit, based on a computed average round-trip time. The initial retry time is computed based on the measured roundtrip time of establishing the session. The retransmission algorithm doubles the delay with every attempt, for example, if we set the MaxRetryCount to 8(default), if WCF determines the initial retry time to be 2 seconds, then 8th retry will set the retry ackonwledgement time to be 2^8 which is 512 seconds. We can not change the acknowledgement interval manually. so if you see from &lt;font face=""&gt;Fiddler, WCF &lt;font face=""&gt;Visualizer&lt;/font&gt; or Trace Viewer, it is not surprise to see some messages been sent several times if you use &lt;span class="identifier"&gt;ReliableSession. There is no other way to avoid this except making server stateless (disable ReliableSession)&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=""&gt;&lt;span class="identifier"&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt; &lt;img src="http://geekswithblogs.net/argot/aggbug/135730.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>argot</dc:creator>
            <guid>http://geekswithblogs.net/argot/archive/2009/10/27/wcf-reliablesession-retry-interval.aspx</guid>
            <pubDate>Tue, 27 Oct 2009 04:45:50 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/argot/comments/135730.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/argot/archive/2009/10/27/wcf-reliablesession-retry-interval.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/argot/comments/commentRss/135730.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>
