wcf 条件压缩 [英] wcf conditional compression

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

问题描述

我最近插入了一个自定义编码器(使用二进制编码器进行实际编码,使用 Gzip 压缩器来压缩字节数组).它工作正常.现在的问题是对于小消息大小,它实际上会使字节数组膨胀.我想知道是否有办法避免这种情况.特别是如果有一种方法可以应用条件压缩和解压缩.

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.

以下是发生压缩和解压缩的函数(来自 CompactMessageEncoder 的代码片段)

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;
        }

推荐答案

如果您使用的是 http 并且您使用的是 IIS 7 或更高版本(也可以使用 6,但显然配置更难),您可以使用内置 gzip/deflate 压缩.

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.

参见 http://www.iis.net/ConfigReference/system.webServer/http压缩

该链接还解释了诸如minFileSizeForComp"之类的参数,这些参数正好解决了您的问题.它还具有许多其他不错的参数,例如dynamicCompressionDisableCpuUsage",一旦超过某个 cpu 负载,它将禁用压缩.

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.

http 压缩的好处在于它是一个标准,客户端和服务器可以确定每个请求是否可以或想要使用标准的 http 请求和响应标头进行压缩.此外,压缩格式可以从 gzip 更改为 deflate,后者有几个优点,尽管使用不多.看http://madskristensen.net/post/Compression-and-性能-GZip-vs-Deflate.aspxhttp://www.vervestudios.co/projects/compression-tests/

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/

一个潜在的缺点 - 取决于您的应用程序,http 压缩仅从服务器 -> 客户端完成,所以如果您的请求很大,这可能会成为一个问题,但在大多数情况下,服务器响应会更大比客户要求.如果您愿意添加自定义IHttpModule,您甚至可以获得请求压缩工作.

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. If you are willing to add a custom IHttpModule, you can even get request compression working.

我刚刚成功地将大量使用的 20 台服务器从 GZIPMessageEncoder 迁移到 IIS 7.5 的 http 压缩.

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

附带说明,请不要在缓冲传输模式下使用 MS 的 GzipMessageEncoder 示例,这是默认设置.GzipMessageEncoder 只会浪费大量内存 - 在我的特定情况下为数百兆字节,请在此处查看我的答案:WCF HttpTransport:流式传输与缓冲传输模式

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 条件压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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