views:

330

answers:

4

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?

+2  A: 

how about

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

catch(SoapException)
{
 throw new FaultException(); // something similar
}
Asad Butt
I probably shouldn't have said "wrap the SoapException into a FaultException" (edited out now) as that was quite misleading. Of course I want the FaultException to preserve all the properties of the SoapException (i.e. code, message, detail, etc.), so that the unit tests can run the existing asserts on the error handling code.
hheimbuerger
A: 

You should be able to use EnterpriseLibrary to convert the FaultException to a SoapException at a particular boundary, in this case client proxy.

Pratik
Again, I want to convert *from* a SoapException *to* a FaultException, not the other way around! But I'll look into the EnterpriseLibrary, thanks.
hheimbuerger
A: 

You can try to implement IErrorHandler Interface. In HandleError method, rethrow exception wrapped to SoapException. Latter you have to register it globally(web.config or whatever).

Example of how this will help (pseudo code):

public SoapErrorHandler : IErrorHandler
{
   public void HandleError(Exception ex)
   {
      throw new SoapException(ex.Message, ex);
   }
}
Mike Chaliy
I'm not sure I understand how that would help me to convert *from* a SoapException *to* a FaultException.
hheimbuerger
I've added pseudo code example, hope this helps.
Mike Chaliy
Sorry, still the wrong direction. :) I guess this would help if I wanted to convert a FaultException into a SoapException. However, I do not, as stated above.
hheimbuerger
+1  A: 

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.

JoeBilly
JSBangs, I'll rely on you to tell me whether this works for you, in which case I'll mark it as an accepted answer. (Only I can hand out your bounty now, correct?)
hheimbuerger