使用WCF通过线路传输最少量的数据

时间:2009-08-28 14:37:43

标签: c# .net wcf

我的项目有一个netTCP WCF服务。 这是它的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>

是否可以采取任何措施来最大限度地压缩通过线路发送的数据?我的项目是内部的,因此速度和处理能力基本上没有问题。

压缩从客户端发送到WCF服务的数据有哪些好的提示和技巧?

4 个答案:

答案 0 :(得分:8)

绑定指定的消息编码将决定您的数据如何在线路上转换为字节。对于NetTcpBinding,它将自动使用二进制编码,从而为您提供所有内置WCF编码器中最紧凑的消息表示。

有关详细信息,我建议使用以下资源:

  1. Zulfiqar Ahmed:SOAP message size optimization: Encoding vs compression
  2. Kenny Wolf:Performance Characteristics of WCF Encoders
  3. MSDN:Choosing a Message Encoder

答案 1 :(得分:2)

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

答案 2 :(得分:2)

我仍然试图将所有这些拼凑在一起,但我知道当你使用DataContractAttribute时,你正在使用DataContract序列化。我不清楚这个序列化方案和Serializable方案之间的区别,但是从我能够收集到的,它们是不同的。

Mar的主持人之一Marc Gravell是我在这个问题上看过的专家。他实际上有一个名为protobuf-net的序列化方案,可以使用here

答案 3 :(得分:0)

  1. 使用压缩..在4.5           

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

  2. 不要使用命名空间set namespace to“”(以及服务合同上)。   [DataContract(Namespace =“”)]    公共类AddDeckMessage

  3. 很少(如果在XML上发送接口/基类)...... XML不理解,它添加了Microsoft explcit XML。不要使用knowntype使用普通的DTO,你可以自定义线...

  4. 使用EmitDefaultValue

  5. 小心使用非tcp压缩的byte []。如果你看到它为123,你会看到每个字节15-30个字节,具体取决于编码。如果需要使用标准WS协议,请使用uuencode。

相关问题