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

我已经修改了内部故障和错误处理,以处理FaultExceptions而不是SoapExceptions。

该项目具有广泛的测试用例套件,用于测试仍然依赖于SoapException的错误和错误处理。由于种种原因,我不希望全部重写它们。

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

最佳答案

使用消息检查器呢?您检查了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是一个好习惯。

关于.net - 使用WCF是否可以将SoapException转换为FaultException?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2332978/

10-13 06:56