是的,消息将被传输二进制,但序列化器(我假设为 Datacontractserializer)将以 XML 格式序列化数据:
使用 DataContractSerializer 类将类型的实例序列化和反序列化为 XML 流或文档
DataContractSerializer
来自文档:
默认情况下,NetTcpBinding 会生成一个运行时通信堆栈,它使用传输安全、TCP 进行消息传递和二进制消息编码。此绑定是一个适当的系统提供的选择,用于通过内联网进行通信。
NetTcpBinding MSDN
如果您选择实现 ISerializable,您也可以使用 WCF,但您必须实现 DataContractResolver 来解析类型:如果客户端“知道”类型(例如,您将它们放入 dll 并将它们添加到客户端 -应用程序)您可以使用以下示例代码(对不起,我只在 F# 中使用它,但您应该会发现它很容易翻译)
这应该以更紧凑的形式产生序列化。
type internal SharedTypeResolver() =
inherit System.Runtime.Serialization.DataContractResolver()
let dict = new Xml.XmlDictionary()
override this.TryResolveType(t : Type, declaredT : Type, knownTypeResolver : System.Runtime.Serialization.DataContractResolver, typeName : Xml.XmlDictionaryString byref, typeNamespace : Xml.XmlDictionaryString byref) =
typeNamespace = dict.Add(t.Assembly.FullName)
typeName = dict.Add(t.FullName)
true
override this.ResolveName(typeName : string, typeNamespace : string, declaredType : Type, knownTypeResolver : System.Runtime.Serialization.DataContractResolver) =
let res = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null)
if res = null then Type.GetType(typeName + ", " + typeNamespace) else res
PS:在 C# 中发现相同:
public class SharedTypeResolver : DataContractResolver
{
#region Overrides of DataContractResolver
///
/// Override this method to map a data contract type to an xsi:type name and namespace during serialization.
///
///
/// true if mapping succeeded; otherwise, false.
///
/// The type to map.The type declared in the data contract.The known type resolver.The xsi:type name.The xsi:type namespace.
public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
{
if (!knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace))
{
var dict = new XmlDictionary(); // nice trick to get the right type for typeName
if (type != null)
{
typeNamespace = dict.Add(type.Assembly.FullName);
typeName = dict.Add(type.FullName);
}
else
{
typeNamespace = dict.Add("noAss");
typeName = dict.Add("noType");
}
}
return true;
}
///
/// Override this method to map the specified xsi:type name and namespace to a data contract type during deserialization.
///
///
/// The type the xsi:type name and namespace is mapped to.
///
/// The xsi:type name to map.The xsi:type namespace to map.The type declared in the data contract.The known type resolver.
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
{
return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null) ??
Type.GetType(typeName + ", " + typeNamespace);
}
(请注意:stackoverflow 不喜欢 F# 中的赋值运算符“
private static void AddResolver(OperationDescription operationDescription)
{
if (operationDescription == null)
throw new ArgumentNullException();
var serializationBehavior = operationDescription.Behaviors.Find();
if (serializationBehavior == null)
{
serializationBehavior = new DataContractSerializerOperationBehavior(operationDescription);
operationDescription.Behaviors.Add(serializationBehavior);
}
serializationBehavior.DataContractResolver = new SharedTypeResolver();
}
将此用于:
var contrDescription = _host.Description.Endpoints[0].Contract;
var description= contrDescription.Operations.Find("MyServiceMethod");
AddResolver(description);
用您的服务方法的名称替换“MyServiceMethod”(按方法调用或遍历所有方法)