繁体   English   中英

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

[英]Transmitting the least amount of data over the wire with 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服务的数据有哪些好的技巧和窍门?

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

有关更多信息,我建议以下资源:

  1. Zulfiqar Ahmed: SOAP消息大小优化:编码与压缩
  2. 肯尼·沃尔夫(Kenny Wolf): WCF编码器的性能特征
  3. MSDN: 选择消息编码器

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

我仍在尝试将所有这些整合在一起,但我确实知道,当您使用DataContractAttribute时,您正在使用DataContract序列化。 我不清楚这个序列化方案和Serializable方案之间的区别,但是根据我的了解,它们是不同的。

SO的主持人之一Marc Gravell是我一直致力于解决此问题的专家。 他实际上有一个称为protobuf-net的序列化方案,可在此处使用。

  1. 使用压缩..在4.5中

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

  2. 不要使用将命名空间设置为“”的命名空间(以及在服务协定上也是如此)。[DataContract(Namespace =“”)]公共类AddDeckMessage

  3. 很少(如果曾经在XML上发送接口/基类)... XML无法理解,并且会添加Microsoft引爆的XML。 请勿使用可自定义导线的已知类型使用普通DTO ...

  4. 使用EmitDefaultValue

  5. 注意使用非tcp压缩的byte []。 如果将其视为123,则每个字节将看到15-30个字节,具体取决于编码。 如果需要使用标准WS协议,请使用uuencode。

暂无
暂无

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

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