简体   繁体   English

就传输的总字节数而言,最紧凑的wcf绑定是什么?

[英]What is the most compact wcf binding in terms of total bytes transferred?

We need to create a WCF service for a low bandwidth network. 我们需要为低带宽网络创建WCF服务。 So our goal is to create the binding configuration which produces the smallest number of bytes per call. 因此,我们的目标是创建一个绑定配置,使每个调用产生的字节数最少。 This is an excerpt from the app.config 这是摘自app.config

<customBinding>
   <binding name="binaryBinding">
     <binaryMessageEncoding/>
     <tcpTransport />
   </binding>
</customBinding>

I assume that setting up a connection introduces overhead as well, so we would keep that to a minimum. 我假设建立连接也会带来开销,因此我们将其降至最低。 A typical scenario would be 典型的情况是

Client.Connect();
while (!userTerminates())
{
  var Response = Client.DoSomething(parameters);
  <user input>
  var anotherResponse = Client.DoSomethingElse(otherParameters);
}
Client.Close();

The data being sent/received varies from some 20 bytes to a couple of 100. 发送/接收的数据从20字节到100左右不等。

So far I managed to setup WCF to use netTcpBinding and binary encoding, but I still have an overhead of 400%. 到目前为止,我设法将WCF设置为使用netTcpBinding和二进制编码,但是我仍然有400%的开销。 I sent a single string of 600 chars and received 2200. I measured this by using a MessageInspector which measures the length of incoming and outgoing messages. 我发送了600个字符的单个字符串,并接收了2200个字符。我使用MessageInspector进行了测量,该工具测量传入和传出消息的长度。 I assume that the overhead comes from the SOAP messages. 我假设开销来自SOAP消息。 Edit: Maybe my measuring is wrong. 编辑:也许我的测量是错误的。

I also understand that I can compress the communication as shown in the WCF Sample library. 我也了解我可以按照WCF示例库中所示压缩通信。 GoogleProtocol also seems an option. GoogleProtocol似乎也是一种选择。

But I want to start from the beginning: How can I modify the configuration (or eventually create my own protocol) to minimize the total bytes sent. 但是,我想从头开始:如何修改配置(或最终创建自己的协议)以最大程度地减少发送的总字节数。 Security is not an issue at this time, neither is performance, it's only the number of bytes being sent/received I am interested in. 目前,安全性不是问题,性能也不是问题,这只是我感兴趣的发送/接收的字节数。

Thanks for any hints. 感谢您的任何提示。

You're right in that netTcp uses SOAP , which may indeed cause your 20 bytes payload to inflate to more than that. 您是对的, netTcp使用SOAP ,这确实可能导致20字节的有效负载膨胀得更多。

How to apply a custom binding is explained in CodeProject: CompactMessageEncoder . CodeProject:CompactMessageEncoder中说明了如何应用自定义绑定。 If the payload is object-like and you don't call many difference methods, Google's Protocol Buffers and a custom framing protocol over sockets . 如果有效负载是类对象,并且您不需要调用许多不同的方法,则可以使用Google的协议缓冲区套接字上自定义框架协议

The overhead is caused by the fact that WCF serializes objects as XML streams. 开销是由WCF将对象序列化为XML流的事实引起的。 This means it makes no assumptions about the order or format of the fields it serializes and needs to provide this information with the message. 这意味着它不对其序列化的字段的顺序或格式做任何假设,并且需要将此信息与消息一起提供。 The binary message encoders use the XmlDictionaryReader/Writer to serialize messages and use dictionaries to look up fields that have been transmitted earlier in the session. 二进制消息编码器使用XmlDictionaryReader / Writer来序列化消息,并使用字典来查找会话中较早发送的字段。 Still, at some point this information is communicated between client and server which causes extra bytes to be transmitted depending on the size of your data contract. 仍然,在某些时候,此信息在客户端和服务器之间进行通信,这取决于数据合同的大小,导致额外的字节被传输。

To reduce payload, consider writing your own serializer as shown here: http://www.codeproject.com/Articles/434665/WCF-Serialization-A-Case-Study . 为了减少有效负载,请考虑编写您自己的序列化器,如下所示: http : //www.codeproject.com/Articles/434665/WCF-Serialization-A-Case-Study

Also you may consider packaging your data in a single field prior to serialization in the OnSerializing and OnDeserialized methods like so: 另外,您可以考虑在序列化之前使用OnSerializing和OnDeserialized方法将数据打包在单个字段中,如下所示:

[DataContract]
public class Data
{
    [DataMember]
    private byte data;

    private bool bool1;
    private bool bool2;

    [OnSerializing]
    internal void OnSerializing()
    {
        data = Convert.ToByte((bool1 ? 0 : 1) & (bool2 ? 0 : 2));
    }

    [OnDeserialized]
    internal void OnDeserializing()
    {
        bool1 = (Convert.ToInt32(data) & 1) != 0;
        bool2 = (Convert.ToInt32(data) & 2) != 0;
    }
}

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

相关问题 什么是<readerquotas>在 WCF 绑定?</readerquotas> - what is <readerQuotas> in WCF Binding? 如何从sql server播放mp3字节块,使用C#转移到桌面客户端(通过wcf)? - how to play chunks of mp3 bytes from sql server, transferred to desktop client (by wcf) using C#? 创建IEnumerable <T>或ICollection <T>的最简单,最紧凑的方法是什么? - What is the easiest and most compact way to create a IEnumerable<T> or ICollection<T>? WCF中的基础架构和应用程序端点之间有什么区别(使用非专业术语)? - What is the difference between Infrastructure and application endpoints in WCF (in lay terms)? WCF绑定我不明白什么是错 - WCF binding I not understanding what is wrong C#SqlCommand字节传输大小 - C# SqlCommand bytes transferred size 获取通过编码创建的字节数的最有效方法是什么? - What is the most efficient way to get the amount of bytes created from encoding? 使用索引从列表中提取多个(非连续)值的最紧凑和/或最有效的方法是什么 - What is the most compact and/or efficient way to extract multiple (non-consecutive) values from a list using indexing 从Monodroid连接到WCF服务的最安全方法是什么? - What is the most secure way to connect to a WCF Service from monodroid? 用于绑定的xaml术语的差异 - Difference in xaml terms used for binding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM