如何有效地使用 WCF 发送 byte[] [英] How to send byte[] using WCF efficiently

查看:73
本文介绍了如何有效地使用 WCF 发送 byte[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 WCF 服务发送 byte[] 数组,我关心消息大小和性能.我的问题是:WCF 是否在内部将我的二进制数据转换为 Base64 字符串?(我使用netTcpBinding)如果是,如何防止?

I need to send byte[] array using WCF services and I care about message size and performance. My question is: Does WCF convert my binary data to Base64 string internally? (I use netTcpBinding) If it does, how to prevent it?

我的合同看起来几乎一样简单

My contract looks almost as simple as

IMyService 
{
  byte[] GetData()
}

推荐答案

就实际的 XML 而言,字节数组被序列化为 xs:base64Binary,这是一种 Base64 编码(显然).

In terms of the actual XML, byte arrays are serialized as xs:base64Binary, which is a Base64 encoding (obviously).

但是,当您指定 netTcpBinding 时,WCF 使用该 XML 的二进制消息编码 (BinaryMessageEncodingBindingElement) 将这些字节编码为二进制流.

However, when you specify the netTcpBinding, WCF uses a binary message encoding of that XML (BinaryMessageEncodingBindingElement) which will encode those bytes as a binary stream.

这是一个有点复杂的过程,但归根结底,如果您使用任何 net* 绑定,字节数组将作为原始字节通过网络,而不是base64 字符串.如果可以,这就是使用 TCP 或命名管道绑定通常更有效的众多原因之一.

It's a bit of a convoluted process, but at the end of the day, if you're using any of the net* bindings, byte arrays will go over the wire as raw bytes, not base64 strings. That's one of many reasons why it's generally more efficient to use the TCP or Named Pipe bindings, if you can.

这篇关于如何有效地使用 WCF 发送 byte[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆