假设我有以下请求对象:
[DataContract]
public class MyContract {
[DataMember]
public Guid Token { get; set; }
}
和一个 WCF 服务定义如下:
[ServiceContract]
public interface IMyService {
[OperationContract]
bool Validate(MyContract request);
}
如果我将以下内容发送到操作,我会得到所需的响应:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:esi="http://mynamespace.com/" xmlns:con="http://mynamespace.com">
<soapenv:Header>
</soapenv:Header>
<soapenv:Body>
<esi:Validate>
<esi:Token>9192ef6a-819f-4a8a-8fde-4125999e33dc</esi:Token>
</esi:Validate>
</soapenv:Body>
</soapenv:Envelope>
如果我发送无效的 Guid(任何类型都会发生这种情况),我会收到以下响应:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode>
<faultstring xml:lang="en-GB">The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>
考虑到我的服务确切地知道数据出了什么问题,这一切都很好,但对我的消费者来说信息还不够。
我可以使用 web 配置设置公开完整的异常<serviceDebug includeExceptionDetailInFaults="true"/>
,但这对我的消费者来说信息太多了!我宁愿在代码级别自定义错误,但我不确定如何附加到反序列化器?我知道如何处理自定义 SOAP 错误和 FaultContract,但这似乎处于较低级别 - 我需要在传入消息到达 CLR 方法之前拦截它,不知何故?有没有办法做到这一点,我不知道?