简体   繁体   English

使用WCF通过网络传输最少的数据

[英]Transmitting the least amount of data over the wire with WCF

My project has a netTCP WCF service. 我的项目有一个netTCP WCF服务。 This is the app.config for it: 这是它的app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_IIndexer" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                    hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                    maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
                    maxReceivedMessageSize="65536">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="None">
                        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://mach1:9000/Indexer" binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_IIndexer" contract="in.IIndexer"
                name="NetTcpBinding_IIndexer" />
        </client>
    </system.serviceModel>
</configuration>

Is there any thing that can be done to maximize the compression of the data being sent over the wire? 有什么可以做的事情来最大程度地压缩通过网络发送的数据? My project is internal so speed and processing power are essentially of no issue. 我的项目是内部项目,因此速度和处理能力基本上没有问题。

What are some good tips and tricks to compress the data sent from the client to the WCF service? 压缩从客户端发送到WCF服务的数据有哪些好的技巧和窍门?

The message encoding specified by the binding will determine how your data gets turned into bytes on the wire. 绑定指定的消息编码将确定如何将数据转换为在线上的字节。 For the NetTcpBinding, it will automatically use binary encoding which gives you the most compact representation of your message out of all the built-in WCF encoders. 对于NetTcpBinding,它将自动使用二进制编码,从而为您提供所有内置WCF编码器中最紧凑的消息表示形式。

For more information, I would recommend these resources: 有关更多信息,我建议以下资源:

  1. Zulfiqar Ahmed: SOAP message size optimization: Encoding vs compression Zulfiqar Ahmed: SOAP消息大小优化:编码与压缩
  2. Kenny Wolf: Performance Characteristics of WCF Encoders 肯尼·沃尔夫(Kenny Wolf): WCF编码器的性能特征
  3. MSDN: Choosing a Message Encoder MSDN: 选择消息编码器

这取决于您要发送的数据类型,但是如果您要使用序列化来创建数据,则序列化为XML并使用GZipStream压缩它所产生的字节数要比压缩二进制序列化所生成的数据少。

I'm still trying to piece all of this together myself, but I do know that when you use the DataContractAttribute, you are using DataContract serialization. 我仍在尝试将所有这些整合在一起,但我确实知道,当您使用DataContractAttribute时,您正在使用DataContract序列化。 I'm not clear exactly on the differences between this serialization scheme and the Serializable scheme, but from what I've been able to gather, they are different. 我不清楚这个序列化方案和Serializable方案之间的区别,但是根据我的了解,它们是不同的。

Marc Gravell, one of the moderators here at SO, is the expert that I've looked to on this issue. SO的主持人之一Marc Gravell是我一直致力于解决此问题的专家。 He actually has a serialization scheme called protobuf-net that is available for use here . 他实际上有一个称为protobuf-net的序列化方案,可在此处使用。

  1. Use compression .. In 4.5 使用压缩..在4.5中

      <binaryMessageEncoding compressionFormat="GZip"/> <tcpTransport maxReceivedMessageSize="20000000"/> </binding> 

  2. Do not use namespaces set namespace to "" (and on the service contract as well.) [DataContract(Namespace = "" )] public class AddDeckMessage 不要使用将命名空间设置为“”的命名空间(以及在服务协定上也是如此)。[DataContract(Namespace =“”)]公共类AddDeckMessage

  3. Rarely ( if ever send Interfaces / base classes on XML) ... XML does not understand and it adds Microsoft explcit XML. 很少(如果曾经在XML上发送接口/基类)... XML无法理解,并且会添加Microsoft引爆的XML。 Do not use knowntype use plain DTOs which you can customize to the wire... 请勿使用可自定义导线的已知类型使用普通DTO ...

  4. Use EmitDefaultValue 使用EmitDefaultValue

  5. Be careful with byte[] with non tcp compression. 注意使用非tcp压缩的byte []。 If you see it as 123 your seeing 15-30 bytes for each byte depending on encoding. 如果将其视为123,则每个字节将看到15-30个字节,具体取决于编码。 Use uuencode if you need to use standard WS protocols. 如果需要使用标准WS协议,请使用uuencode。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM