我已经修改了内部错误和错误处理来处理FaultExceptions而不是SoapExceptions。
该项目有一套广泛的测试用例来测试仍然依赖于SoapException的错误和错误处理。出于各种原因,我宁愿不重写它们。
是否可以将SoapException转换为FaultException,从而针对新的错误处理代码运行旧的测试用例?
答案 0 :(得分:2)
怎么样
抓住SoapException
并投掷FaultException
(解决方案,而非推荐)
catch(SoapException)
{
throw new FaultException(); // something similar
}
答案 1 :(得分:1)
使用消息检查器怎么样?你检查了IClientMessageInspector吗?
看起来像这样:
消息检查器
public class MessageInspector : IClientMessageInspector
{
...
#region IClientMessageInspector Members
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
//rethrow your exception here, parsing the Soap message
if (reply.IsFault)
{
MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
Message copy = buffer.CreateMessage();
reply = buffer.CreateMessage();
object faultDetail = //read soap detail here;
...
}
}
#endregion
...
}
端点行为
public class MessageInspectorBehavior : IEndpointBehavior
{
...
#region IEndpointBehavior Members
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
MessageInspector inspector = new MessageInspector();
clientRuntime.MessageInspectors.Add(inspector);
}
#endregion
...
}
http://weblogs.asp.net/paolopia/archive/2007/08/23/writing-a-wcf-message-inspector.aspx
我认为一个好的做法是use exceptions as faults too。
答案 2 :(得分:0)
您应该能够使用EnterpriseLibrary将FaultException转换为特定边界的SoapException,在本例中为客户端代理。
答案 3 :(得分:-1)
您可以尝试实施IErrorHandler Interface。在HandleError方法中,重新抛出包含在SoapException中的异常。后来你必须在全球注册它(web.config或其他)。
这将如何帮助的示例(伪代码):
public SoapErrorHandler : IErrorHandler
{
public void HandleError(Exception ex)
{
throw new SoapException(ex.Message, ex);
}
}