WCF - (自定义)二进制序列化
我希望能够通过网络查询我的数据库,并且我希望使用 WCF 服务来处理请求和结果。问题是,由于这些查询可能返回的数据量很大,我担心这些结果将如何通过网络序列化。例如,我可以想象 XML 序列化如下所示:
<Results>
<Person Name="Adam" DateOfBirth="01/02/1985" />
<Person Name="Bob" DateOfBirth="04/07/1986" />
</Results>
二进制序列化包含类型名称和其他(不必要的)元数据。也许甚至是集合中每个元素的类型名称? o_o
理想情况下,我想自己执行某些“DataContract”的序列化,这样我就可以使其超级紧凑。有谁知道这是否可行,或者有任何文章解释如何使用 WCF 进行自定义序列化?
提前致谢
I want to be able to query my database over the web, and I am wanting to use a WCF service to handle the requests and results. The problem is that due to the amount of data that can potentially be returned from these queries, I'm worried about how these results will be serialised over the network. For example, I can imagine the XML serialisation looking like:
<Results>
<Person Name="Adam" DateOfBirth="01/02/1985" />
<Person Name="Bob" DateOfBirth="04/07/1986" />
</Results>
And the binary serialisation containing types names and other (unnecessary) metadata. Perhaps even the type name for each element in a collection? o_o
Ideally, I'd like to perform the serialisation of certain 'DataContract'-s myself so I can make it super-compact. Does anyone know if this is possible, or of any articles which explain how to do custom serialisation with WCF?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WCF 支持开箱即用的二进制序列化格式 - 默认情况下在
netTcpBinding
中使用它。我建议首先尝试将其用于您的测试 - 仅当它不起作用并且压缩不够,并且您确信自己可以比整个 WCF 团队更聪明、更编程时,才可以开始实施您自己的自定义二进制序列化格式。查看一些优秀资源:
利用 WCF 绑定的可组合性,您还可以轻松创建二进制 HTTP 绑定等。
这里的主要问题是:如果你做这样的事情,通信渠道的两端必须就如何完成事情达成一致,例如你不能指望一个简单的浏览器能够连接并理解你的自定义二进制编码 - 你需要能够控制线路的客户端和服务器端。
WCF support a binary serialization format out of the box - it's used by default in the
netTcpBinding
. I would recommend to try and use that for your tests first - only if that doesn't work and doesn't compress enough, and you're confident you can outsmart and outprogram the entire WCF team, go and roll your own custom binary serialization format.Check out some excellent resources:
With the composability of WCF bindings, you could easily also create a e.g. binary HTTP binding.
The main issue here will be: if you do something like this, both ends of the communication channel must agree on how things are done, e.g. you cannot expect a simple browser to be able to connect and understand your custom binary encoding anymore - you need to be able to control the client and the server side of the wire.