【发布时间】:2015-12-31 23:16:19
【问题描述】:
我正在尝试使用 DataContractResolver 作为 WCF 中 KnownTypes 的替代方法。
我有以下代码,我之前在服务器端使用过它。但是在客户端,在操作行为集合中查找DataContractSerializerOperationBehavior时,代码返回null。
public override IMyService CreateProxy(Uri url)
{
ServiceEndpoint endpoint = CreateEndpoint(url);
var channelFactory = new ChannelFactory<IMyService>(endpoint);
InjectResolver(channelFactory.Endpoint);
return channelFactory.CreateChannel();
}
private void InjectResolver(ServiceEndpoint endpoint)
{
foreach (OperationDescription operation in endpoint.Contract.Operations)
{
var behavior = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
behavior.DataContractResolver = new DerivedTypeResolver(); // behavior is null here!
}
}
为什么缺少该行为?
更新:我发现真正的问题是 WCF 使用的是 XmlSerializer 而不是 DataContractSerializer。有没有办法强制 DataContractSerializer 代替? WCF 是否根据 wsdl 选择序列化程序?考虑到我(还)没有能力改变服务器端,我的选择是什么? XmlSerializer 行为似乎没有自己解决类型的类似选项。
【问题讨论】:
标签: wcf