【发布时间】:2013-08-18 18:34:39
【问题描述】:
当我尝试抛出 FaultException<T> 时,如果我手动生成消息,我会得到正确的 SOAP 消息。但是,当我从服务器上的方法中抛出 FaultException<T> 时,我没有得到正确的 SOAP 消息。 SOAP 消息的 Header / Action 值为http://www.w3.org/2005/08/addressing/soap/fault,而我的 Body / Fault / Detail 元素完全丢失。
请查看我的代码的重要部分。
服务合同
[ServiceContract(Namespace = "http://namespace.com/fault")]
public interface IFaultService
{
[OperationContract(IsOneWay = false)]
[FaultContract(typeof (MyFault))]
void ThrowFaultException();
}
实施 (message_fault 和 message 是手动创建的,仅供测试使用)
public class FaultService : IFaultService
{
public void ThrowFaultException()
{
var fault = new MyFault("This is from the service");
var fault_exception = new FaultException<MyFault>
(fault,
"Fault Service Reason",
FaultCode.CreateReceiverFaultCode("FaultCodeName","http://namespace.com/fault"),
"http://namespace.com/fault/IFaultService/ThrowFaultExceptionMyFaultFault");
var message_fault = fault_exception.CreateMessageFault();
var message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, message_fault,
fault_exception.Action);
throw fault_exception;
}
}
数据合同
[DataContract(Namespace = "http://namespace.com/fault")]
public class MyFault
{
public MyFault(string Message)
{
this.Message = Message;
}
[DataMember]
public string Message { get; set; }
}
web.config system.serviceModel 部分
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttpBindingNoSecurity">
<security mode="None"/>
</binding>
</wsHttpBinding>
<services>
<service name="FaultService">
<endpoint address="ws"
binding="wsHttpBinding"
bindingConfiguration="wsHttpBindingNoSecurity"
bindingNamespace="http://namespace.com/fault"
contract="IFaultService"
name="ws">
</endpoint>
</service>
</services>
...
</system.serviceModel>
手动生成的 SOAP 消息
{<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://namespace.com/fault/IFaultService/ThrowFaultExceptionMyFaultFault</a:Action>
</s:Header>
<s:Body>
<s:Fault>
<s:Code>
<s:Value>s:Receiver</s:Value>
<s:Subcode>
<s:Value xmlns:a="http://namespace.com/fault">a:FaultCodeName</s:Value>
</s:Subcode>
</s:Code>
<s:Reason>
<s:Text xml:lang="en-US">Fault Service Reason</s:Text>
</s:Reason>
<s:Detail>
<MyFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://namespace.com/fault">
<Message>This is from the service</Message>
</MyFault>
</s:Detail>
</s:Fault>
</s:Body>
</s:Envelope>}
但实际遇到的 SOAP 是
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.w3.org/2005/08/addressing/soap/fault</a:Action>
<a:RelatesTo>urn:uuid:a3587103-953a-43f6-9691-951c21de8418</a:RelatesTo>
<ActivityId CorrelationId="23bf0a82-f4ac-4227-85ab-600f6ffe6a6f" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">a0945fa9-f5a3-4958-8682-097a8315f0dc</ActivityId>
</s:Header>
<s:Body>
<s:Fault>
<s:Code>
<s:Value>s:Sender</s:Value>
</s:Code>
<s:Reason>
<s:Text xml:lang="en-US">Fault Service Reason</s:Text>
</s:Reason>
</s:Fault>
</s:Body>
</s:Envelope>
提前致谢。
【问题讨论】:
-
请尝试通过在 web.config 文件中添加 System.Diagnostics 标记来生成 svclog 文件。这样你就会得到确切的问题。
-
感谢您的回复 - 我的示例中没有包含诊断标签,但我确实在配置中包含了它,我无法发现有关该问题的更多信息。