如何在编码器级别确定 WCF 消息大小
我正在构建一个压缩 WCF 响应的自定义编码器。它基于 Microsoft WCF 示例中的 Gzip 编码器和此博客文章:
http://frenk.wordpress.com/2009/12/04/gzip-compression-wcfsilverlight/
我已经一切正常,但现在我只想在回复超出范围时才应用压缩特定大小,但我不确定如何从编码器级别检索实际消息的总大小。
我需要在 EncoderFactory 中的 WriteMessage(...) 方法和 DispatchMessageInspector 中的 BeforeSendReply(...) 方法中获取消息大小,以便我知道是否压缩消息,以便我可以添加响应的“gzip”ContentEncoding 标头。请求总是很小并且没有压缩,所以我不需要担心这一点。
任何帮助表示赞赏。
乔恩.
I am building a custom encoder that compresses WCF responses. It is based on the Gzip encoder in Microsoft's WCF samples and this blog post:
http://frenk.wordpress.com/2009/12/04/gzip-compression-wcfsilverlight/
I've got it all working, but now I would like to apply the compression only if the reply is beyond a certain size, but I am not sure how to retrieve the total size of the actual message from the encoder level.
I would need to get the message size at both the WriteMessage(...) method in the EncoderFactory, so I know whether to compress the message) and at the BeforeSendReply(...) method in the DispatchMessageInspector so that I can add the "gzip" ContentEncoding header to the response. Requests are always small and not compressed, so I don't need to worry about that.
Any help appreciated.
Jon.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你会分两个阶段来做这件事。首先,编写一个自定义 MessageEncoder,将消息编码为正常的
byte[]
类型。一旦获得编码的字节数组(可以是任何消息编码格式...Xml、Json、二进制等),您就可以检查字节数组大小并确定是否要创建另一个压缩字节数组。您可能会发现一些有用的资源:
byte[]
大小)。您可能可以调整它并创建一个“ThresholdCompressionEncoder”。I think you would do this in two stages. First, write a custom MessageEncoder that encodes the message to a
byte[]
just normal. Once you have the encoded byte-array (and this can be any message encoding format... Xml, Json, binary, whatever) you can examine the byte-array size and determine whether you want to create another compressed byte array.Several resources you may find useful:
byte[]
size). You could probably adapt this and create a "ThresholdCompressionEncoder".您可以尝试根据
reply.ToString.Length()
和message.ToString.Length()
计算它You can try calculating it based on
reply.ToString.Length()
andmessage.ToString.Length()