如何解决WCF REST 中 WebFaultException抛出的异常客户端WebChannelFactory无法处理的问题

使用WebChannelFactory在net中可以方便构造出 WCF REST使用的客户端,但是这个时候有一个问题,就是当我们的服务端有一些错误的时候,
比如验证通不过我们希望可以抛出自定义的异常消息(是我们同一的消息类),这个时候我们会发现客户端无法处理了

他会出现

Unable to deserialize XML body with root name 'ErrorMessage' and root namespace '' (for operation 'Get' and contract ('IService1',  'http://tempuri.org/')) using DataContractSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.

这样的一个错,

这个时候期实我们可以改进一下客户端,让我们所有的实体类继承于这个消息类,这样它就一切ok了

但是对于List<> 和 Void的又没有办法了
后面我们现通过使用

 [ServiceKnownType(typeof(List<SampleItem>))] 和dynamic返回类弄型我们可以实现自己想要的结果了

但是最好的方法是通过自己写一个 WebChannelFactory(它里面实际上是一个Proxy来起作用的)类似的类来实现这样的一个过程
原文地址:https://www.cnblogs.com/lovebanyi/p/1961260.html