WCF有条件的COM pression [英] wcf conditional compression

查看:100
本文介绍了WCF有条件的COM pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在一个自定义的连接codeR插入(使用二进制EN codeR做实际的编码和gzip COM presser对COM preSS字节数组)。它工作正常。现在的问题是对于小规模的消息实际上膨胀字节数组。我想知道是否有避免这种情况的一种方式。具体来说,如果有一种方法,我可以申请条件COM pression和DECOM pression。

我曾尝试做这样的事情 - 将一个条件

 如果(buffer.Count< = 5000)
 跳过COM pression

但问题是在另一端DECOM pression会发生即使字节不是玉米pressed。我希望这是有道理的。

以下是哪里COM pression和DECOM pression发生(从CompactMessageEn codeR code段)的功能

 公众覆盖信息ReadMessage(ArraySegment<位>缓冲区,BufferManager bufferManager,字符串的contentType)
        {            ArraySegment<位> DECOM pressedBuffer = DECOM pressBuffer(缓冲,bufferManager);
            LogWrite(DECOM pressed从{0}字节{1}字节,buffer.Count,DECOM pressedBuffer.Count);            消息returnMessage = _innerEn coder.ReadMessage(DECOM pressedBuffer,bufferManager);            returnMessage.Properties.En codeR =这一点;
            返回returnMessage;
        }公共覆盖ArraySegment<位>的WriteMessage(消息消息,INT maxMessageSize,BufferManager bufferManager,INT messageOffset)
        {
            VAR缓冲= _innerEn coder.WriteMessage(消息,maxMessageSize,bufferManager,messageOffset);            VAR COM pressedBuffer = com的pressBuffer(缓冲,bufferManager,messageOffset);
            LogWrite(的COM pressed从{0}字节{1}字节,buffer.Count,玉米pressedBuffer.Count);            返回COM pressedBuffer;
        }


解决方案

如果您正在使用HTTP,如果你是在IIS 7或更高版本(它也可以有6个,但配置是很难明显地),你可以使用内置的gzip /紧缩COM pression。

请参阅 http://www.iis.net/ConfigReference/system .webServer / httpCom pression

该链接还解释了像'minFileSizeForComp',这正是解决您的问题参数。它也有很多其他不错的参数,如'dynamicCom pressionDisableCpuUsage,这将禁用COM pression一旦某个CPU负载超标。

有关http COM pression的好处是,它是一个标准的客户端和服务器可以每个请求他们是否可以或者想要COM preSS使用标准的HTTP请求和响应头确定。此外,玉米pression格式可以从gzip的尽管不被用作多大改变放气,后者具有两个优点。看到
http://madskristensen.net/post/Com pression和性能,用gzip VS-Deflate.aspx
http://www.vervestudios.co/projects/com$p$pssion -tests /

一个潜在的不利因素 - 根据您的应用程序,是HTTP COM pression只能从服务器上完成 - >客户端,因此,如果您的要求是巨大的,这可能成为一个问题,但是大概在大多数情况下,服务器响应会比客户端请求的方式做大。
编辑:如果你愿意加入定制 IHttpModule的 ,您甚至可以要求COM pression工作。

我刚刚成功迁移的20台服务器从GZIPMessageEn codeR一个频繁使用的农场到IIS 7.5的HTTP COM pression。

在一个侧面说明,请不要在缓冲传输模式,这是默认使用MS'GzipMessageEn codeR实例。 GzipMessageEn codeR只是浪费吨的记忆 - 百兆在我的具体情况,在这里看到我的回答:<一href=\"http://stackoverflow.com/questions/4043683/wcf-httptransport-streamed-vs-buffered-transfermode/6896088#6896088\">WCF HttpTransport:流VS缓冲TransferMode

I recently plugged in a custom encoder (uses binary encoder to do the actual encoding and Gzip compresser to compress the byte array). It works fine. The issue now is for small message size it actually inflates the byte array. I would like to know if there is a way to avoid this. Specifically if there is a way I can apply condition compression and decompression.

I did try to do something like - place a condition

if(buffer.Count <= 5000)
 skip compression

But the problem is on the other end decompression will happen even if the bytes are not compressed. I hope this makes sense.

Following are the functions where compression and decompression happens (code snippet from CompactMessageEncoder)

public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
        {

            ArraySegment<byte> decompressedBuffer = DecompressBuffer(buffer, bufferManager);
            LogWrite("Decompressed from {0} bytes to {1} bytes", buffer.Count, decompressedBuffer.Count);

            Message returnMessage = _innerEncoder.ReadMessage(decompressedBuffer, bufferManager);

            returnMessage.Properties.Encoder = this;
            return returnMessage;
        }



public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
        {
            var buffer = _innerEncoder.WriteMessage(message, maxMessageSize, bufferManager, messageOffset);

            var compressedBuffer = CompressBuffer(buffer, bufferManager, messageOffset);
            LogWrite("Compressed from {0} bytes to {1} bytes", buffer.Count, compressedBuffer.Count);

            return compressedBuffer;
        }

解决方案

In case you are using http and if you are on IIS 7 or higher (it's also possible with 6, but configuration is harder apparently), you could use the builtin gzip / deflate compression.

See http://www.iis.net/ConfigReference/system.webServer/httpCompression

That link also explains parameters like 'minFileSizeForComp' which exactly addresses your issue. It also has a lot of other nice parameters like 'dynamicCompressionDisableCpuUsage', which will disable compression once a certain cpu load is exceeded.

The nice thing about http compression is that it is a standard and client and server can determine per request whether they can or want to compress using standard http request and response headers. Also, the compression format can be changed from gzip to deflate, the latter having a couple of advantages, despite not being used as much. See http://madskristensen.net/post/Compression-and-performance-GZip-vs-Deflate.aspx and http://www.vervestudios.co/projects/compression-tests/

One potential downside - depending on your app, is that http compression is done only from server -> client, so if your requests are huge, that might become an issue, but in probably most cases, server responses will be way bigger than client requests. Edit: If you are willing to add a custom IHttpModule, you can even get request compression working.

I just successfully migrated a heavily used farm of 20 servers from GZIPMessageEncoder to IIS 7.5's http compression.

On a side note, please do not use the MS' GzipMessageEncoder example in buffered transfer-mode, which is the default. GzipMessageEncoder just wastes tons of memory - hundreds of megabytes in my particular case, see my answer here: WCF HttpTransport: streamed vs buffered TransferMode

这篇关于WCF有条件的COM pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆