Message和SOAP Fault
SOAP Fault是SOAP消息的一种形式,用来表示错误信息。Message是对SOAP消息的CLR抽象,可以表示SOAP Fault,如它可以表示一个SOAP消息一样。
SOAP1.2规定SOAP Fault必须包含Code和Reason以及可选的Node、Role和Detail(详细信息)。
Code是发生错误的标识符,也允许使用子节点表示粒度更细的信息。
SOAP1.2定义了Code,还允许程序定义自己的Code。
Reason便是人工可读的错误解释信息,Node表示产生错误的消息参与者。
Role信息表示当错误产生时消息参与者的角色。
Detail是给其他消息参与者准备的错误信息。
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace XMLDictionaryWriter
{
class Program
{
static void Main()
{
FaultCode faultCode = FaultCode.CreateReceiverFaultCode("ReceiverError", "urn:Fault");
FaultReason faultReason = new FaultReason("The value must be > 10");
SomeFaultDetail someFaultDetail = new SomeFaultDetail("Andersoft", "Andersoft.App");
MessageFault messageFault = MessageFault.CreateFault(faultCode, faultReason, someFaultDetail);
CreateAndShowMessage(messageFault, MessageVersion.Soap12WSAddressing10);
//<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">urn:SomeFaultAction</a:Action>
// </s:Header>
// <s:Body>
// <s:Fault>
// <s:Code>
// <s:Value>s:Receiver</s:Value>
// <s:Subcode>
// <s:Value xmlns:a="urn:Fault">a:ReceiverError</s:Value>
// </s:Subcode>
// </s:Code>
// <s:Reason>
// <s:Text xml:lang="en-US">The value must be > 10</s:Text>
// </s:Reason>
// <s:Detail>
// <SomeFaultDetail xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/XMLDictionaryWriter">
// <applicationName>Andersoft.App</applicationName>
// <companyName>Andersoft</companyName>
// <dataOccurred>2013-01-28T11:47:24.872139+08:00</dataOccurred>
// </SomeFaultDetail>
// </s:Detail>
// </s:Fault>
// </s:Body>
//</s:Envelope>
CreateAndShowMessage(messageFault, MessageVersion.Soap11WSAddressing10);
//<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
// <s:Header>
// <a:Action s:mustUnderstand="1">urn:SomeFaultAction</a:Action>
// </s:Header>
// <s:Body>
// <s:Fault>
// <faultcode xmlns:a="urn:Fault">a:ReceiverError</faultcode>
// <faultstring xml:lang="en-US">The value must be > 10</faultstring>
// <detail>
// <SomeFaultDetail xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/XMLDictionaryWriter">
// <applicationName>Andersoft.App</applicationName>
// <companyName>Andersoft</companyName>
// <dataOccurred>2013-01-28T11:48:57.1353644+08:00</dataOccurred>
// </SomeFaultDetail>
// </detail>
// </s:Fault>
// </s:Body>
//</s:Envelope>
}
private static void CreateAndShowMessage(MessageFault messageFault, MessageVersion version)
{
Message message = Message.CreateMessage(version, messageFault, "urn:SomeFaultAction");
Console.WriteLine("{0}\n", message.ToString());
}
}
[Serializable]
struct SomeFaultDetail
{
string companyName;
string applicationName;
DateTime? dataOccurred;
internal SomeFaultDetail(string company, string application)
{
companyName = company;
applicationName = application;
dataOccurred = DateTime.Now;
}
}
}