我正在尝试使用 FaultException 和 FaultException<T> 来确定我们应用程序中的最佳使用模式。我们需要支持 WCF 以及非 WCF 服务消费者/客户端,包括 SOAP 1.1 和 SOAP 1.2 客户端。
仅供参考:使用带有 wsHttpBinding 的 FaultExceptions 会产生 SOAP 1.2 语义,而使用带有 basicHttpBinding 的 FaultExceptions 会产生 SOAP 1.1 语义。
我正在使用以下代码抛出 FaultException<FaultDetails>:
throw new FaultException<FaultDetails>(
new FaultDetails("Throwing FaultException<FaultDetails>."),
new FaultReason("Testing fault exceptions."),
FaultCode.CreateSenderFaultCode(new FaultCode("MySubFaultCode"))
);
FaultDetails 类只是一个简单的测试类,它包含一个字符串“Message”属性,如下所示。
使用 wsHttpBinding 时,响应为:
<?xml version="1.0" encoding="utf-16"?>
<Fault xmlns="http://www.w3.org/2003/05/soap-envelope">
<Code>
<Value>Sender</Value>
<Subcode>
<Value>MySubFaultCode</Value>
</Subcode>
</Code>
<Reason>
<Text xml:lang="en-US">Testing fault exceptions.</Text>
</Reason>
<Detail>
<FaultDetails xmlns="http://schemas.datacontract.org/2004/07/ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Message>Throwing FaultException<FaultDetails>.</Message>
</FaultDetails>
</Detail>
根据 SOAP 1.2 规范,这看起来是正确的。主/根“代码”是“发件人”,其“子代码”为“MySubFaultCode”。如果服务使用者/客户端使用 WCF,客户端上的 FaultException 也模仿相同的结构,faultException.Code.Name 为“Sender”,faultException.Code.SubCode.Name 为“MySubFaultCode”。
使用 basicHttpBinding 时,响应为:
<?xml version="1.0" encoding="utf-16"?>
<s:Fault xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<faultcode>s:MySubFaultCode</faultcode>
<faultstring xml:lang="en-US">Testing fault exceptions.</faultstring>
<detail>
<FaultDetails xmlns="http://schemas.datacontract.org/2004/07/ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Message>Throwing FaultException<FaultDetails>.</Message>
</FaultDetails>
</detail>
</s:Fault>
这看起来不对。查看 SOAP 1.1 规范,当我使用 FaultCode.CreateSenderFaultCode(new FaultCode("MySubFaultCode")) 时,我希望看到“faultcode”的值为“s:Client.MySubFaultCode”。此外,WCF 客户端的结构不正确。faultException.Code.Name 是“MySubFaultCode”而不是“Sender”,faultException.Code.SubCode 是 null 而不是 faultException.Code.SubCode.Name 是“MySubFaultCode”。此外,faultException.Code.IsSenderFault 为假。
使用 FaultCode.CreateReceiverFaultCode(new FaultCode("MySubFaultCode")) 时的类似问题:
- 按预期工作于 SOAP 1.2
- 生成“s:MySubFaultCode”而不是“s:Server.MySubFaultCode”,并且 SOAP 1.1 的 faultException.Code.IsReceiverFault 为 false
这个项目也是2006年别人在http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=669420&SiteID=1上发布的,没有人回答。我很难相信还没有人遇到过这种情况。
这是其他人遇到类似问题:http ://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3883110&SiteID=1&mode=1
Microsoft Connect 错误:https ://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=367963
故障应该如何工作的描述:http: //blogs.msdn.com/drnick/archive/2006/12/19/creating-faults-part-3.aspx
我做错了什么还是这真的是 WCF 中的一个错误?