Is it possible to throw a SoapException into a FaultException with WCF?

I am migrating a web service client from WSE to WCF .

I already changed the internal error and error handling to handle FaultExceptions exceptions instead of SoapExceptions.

The project has an extensive set of test cases for checking errors and handling errors that still depend on SoapException. For various reasons, I would prefer not to rewrite them all.

Is it possible to simply throw a SoapException into a FaultException and thereby run the old test cases with the new error handling code?

+5
source share
4 answers

? 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

, .

+1

SoapException FaultException (, )

catch(SoapException)
{
 throw new FaultException(); // something similar
}
+2

EnterpriseLibrary FaultException SoapException , .

0

You can try to implement the IErrorHandler Interface . In the HandleError method, the rethrow exception is thrown in a SoapException. You need to register the latter globally (web.config or something else).

An example of how this will help (pseudocode):

public SoapErrorHandler : IErrorHandler
{
   public void HandleError(Exception ex)
   {
      throw new SoapException(ex.Message, ex);
   }
}
-1
source

All Articles