NetDataContractSerializer
您在服务器端使用自定义序列化程序。NetDataContractSerializer
和的区别在于DataContractSerializer
序列化增加了Types的附加信息。
例如
DatacontractsSerializer
序列化为:
<Customer xmlns="http://www.contoso.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<FirstName>Zighetti</FirstName>
<ID>101</ID>
<LastName>Barbara</LastName>
</Customer>
NetDataContractSerializer
将相同的对象序列化为:
<Customer z:Id="1" z:Type="NetDCS.Person" z:Assembly="NetDCS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns="http://www.contoso.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<FirstName z:Id="2">Zighetti</FirstName>
<ID>101</ID>
<LastName z:Id="3">Barbara</LastName>
</Customer>
诱导更多类型特定信息。
在您的情况下,服务器知道NetDataContractSerializer
但客户端不知道。您必须通过将其放入共享代码库来与客户端共享此行为。然后,您可以使用以下代码将此 enw 行为添加到客户端:
using (var client = new TestServiceClient())
{
// Get the channel factory
var factory = client.ChannelFactory;
// Attach the behavior
foreach (OperationDescription desc in factory.Endpoint.Contract.Operations)
{
DataContractSerializerOperationBehavior dcsOperationBehavior = desc.Behaviors.Find<DataContractSerializerOperationBehavior>();
if (dcsOperationBehavior != null)
{
int idx = desc.Behaviors.IndexOf(dcsOperationBehavior);
desc.Behaviors.Remove(dcsOperationBehavior);
desc.Behaviors.Insert(idx, new WcfClient.NetDataContractAttribute.NetDataContractSerializerOperationBehavior(desc));
//return true;
}
}
// pass
client.PassInt(0);
// fail
client.PassGuid(Guid.NewGuid());
client.Close();
}
您还可以通过配置文件使用附加行为。