【发布时间】:2014-05-29 16:18:23
【问题描述】:
我正在尝试使用 IErrorHandler 接口在 WCF 中实现自定义错误处理。我想将未处理的异常转换为自定义故障,这是我在 XSD 中获得的结构。
我的代码如下所示:
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (!(error is FaultException))
{
var customFault = new MyCustomFault
{
FaultString = "Something gone wrong",
Detail = new DetailType
{
GeneralFault = new GeneralFaultType
{
Errors = new[]
{
new ErrorType
{
Code = "333"
}
}
}
}
};
var faultException = new FaultException<MyCustomFault>(customFault);
var msgFault = faultException.CreateMessageFault();
fault = Message.CreateMessage(version, msgFault, faultException.Action);
}
}
但是这段代码会产生输出soap消息
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring xml:lang="cs-CZ">The creator of this fault did not specify a Reason.</faultstring>
<detail>
... MyCustomFault goes here...
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
我只能影响故障的细节元素。我想实现,而不是 namespece "http://schemas.xmlsoap.org/soap/envelope/" 中的故障元素将是我的自定义故障对象。这甚至可能吗?我正在使用 XmlSerializer。
谢谢, 迈克尔
【问题讨论】:
标签: .net wcf soap xmlserializer