WCF/WebService: Interoperable exception handling
Asked Answered
O

3

9

I understand that WCF will convert an exception into a fault and send it back as a SOAP message, but I was wondering if this is truly interoperable. I guess I'm having a tough time trying to figure out this possible scenario:

  1. Client (Java) calls a WCF Service (LoginService).
  2. Server checks for proper authorization, user authorization fails.
  3. Server throws an UnauthorizedAccessException.
  4. WCF converts this into a Fault somehow. (* - See Below As Well)
  5. Client has to be able to know how to read this Fault.

I guess I'm just having a tough time understanding how this could still be interoperable because it is expecting Java to know how to translate a SOAP Fault that .NET encodes from an UnauthorizedAccessException.

  • Also, how does .NET actually convert the exception to a fault, what goes in as the fault code, name, etc. Some of the things seem to be "duh"s like perhaps the Fault Name is "UnauthorizedAccessException", but I'd rather know for sure than guess.
Odonto answered 10/1, 2011 at 19:34 Comment(1)
SOAP faults are part of the interoperable, platform-independent SOAP specificiations - so yes, they ARE indeed interoperable! Any client that understands SOAP has to support and understand SOAP faults as well..Metamerism
B
9

There is no "automatic conversion". WCF will return a fault (I forget which one) when you have an unhandled exception. But since you didn't declare that fault, many, if not most, clients will fail if you return it.

You are meant to define your own faults and to return them instead. Consider:

[DataContract]
public class MySpecialFault
{
    public string MyMessage { get; set; }
}

[ServiceContract]
public interface IMyService
{
    [FaultContract(typeof (MySpecialFault))]
    [OperationContract]
    void MyOperation();
}

public class MyService : IMyService
{
    public void MyOperation()
    {
        try
        {
            // Do something interesting
        }
        catch (SomeExpectedException ex)
        {
            throw new FaultException<MySpecialFault>(
                new MySpecialFault {MyMessage = String.Format("Sorry, but {0}", ex.Message)});
        }
    }
}

Any client capable of handling faults will deal with this. The WSDL will define the fault, and they will see a fault with the Detail element containing a serialized version of the MySpecialFault instance that was sent. They'll be able to read all the properties of that instance.

Birdiebirdlike answered 11/1, 2011 at 3:34 Comment(0)
C
4

Faults have been part of the SOAP specification since v1.1. They are explained in the SOAP Specification.

It is up to implementations (WCF, Java etc) to ensure that Faults are handled according to the specification.

Since WCF converts FaultExceptions to Faults according to the SOAP specification, FaultExceptions thrown from WCF are interoperable.

Collencollenchyma answered 10/1, 2011 at 21:52 Comment(1)
Well, I understand that Faults are part of SOAP, but mainly I was wondering if the .NET exception to fault conversion would work out properly.Odonto
E
2

SOAP faults are interoperable but .Net exception classes are not good to be used in SOAP faults. Instead define your own DataContract class (e.g. AccessFault) and then use it in a FaultContract. see http://msdn.microsoft.com/en-us/library/ms733841.aspx

Whenever there is a UnauthorizedAccessException thrown at service boundary convert it to FaultException. This can be done in several ways like using Microsoft Enterprise Library Exception Handling Block or implementing the IErrorHandler interface.

Entrepreneur answered 10/1, 2011 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.