是否可以使用WCF将SoapException转换为FaultException?

Hen*_*ger 5 .net wcf unit-testing

我正在将Web服务客户端从WSE迁移到WCF.

我已经修改了内部错误和错误处理来处理FaultExceptions而不是SoapExceptions.

该项目有一套广泛的测试用例来测试仍然依赖于SoapException的错误和错误处理.出于各种原因,我宁愿不重写它们.

是否可以将SoapException转换为FaultException,从而针对新的错误处理代码运行旧的测试用例?

Joe*_*lly 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

     ...
}
Run Code Online (Sandbox Code Playgroud)

端点行为

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

     ...
}
Run Code Online (Sandbox Code Playgroud)

http://weblogs.asp.net/paolopia/archive/2007/08/23/writing-a-wcf-message-inspector.aspx

我认为一个好的做法是将异常也用作错误