Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to convert a SoapException to a FaultException with WCF?

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

I already modified the internal fault and error handling to deal with FaultExceptions instead of SoapExceptions.

The project has an extensive suite of test cases to test the fault and error handling which still relies on SoapException. For various reasons, I'd prefer not to rewrite them all.

Is it possible to just convert the SoapException into a FaultException and thereby running the old test cases against the new error handling code?

like image 992
Henrik Heimbuerger Avatar asked Nov 06 '22 16:11

Henrik Heimbuerger


2 Answers

how about

catching SoapException and throwing FaultException (a solution, not recommendation)

catch(SoapException)
{
 throw new FaultException(); // something similar
}
like image 185
Asad Avatar answered Nov 14 '22 21:11

Asad


What about using a message inspector ? Have you checked the IClientMessageInspector ?

It may look like this :

The message inspector

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

     ...
}

The endpoint behavior

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

I think a good practice is to use exceptions as faults too.

like image 45
JoeBilly Avatar answered Nov 14 '22 22:11

JoeBilly