Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception handling with WCF Data Services

I want to customize exceptions/errors thrown from my WCF Data Service, so clients get as much as possible information about what exactly went wrong/what is missing. Any thoughts on how this could be achieved?

like image 781
Martinfy Avatar asked Aug 17 '10 10:08

Martinfy


People also ask

What is exception handling in WCF?

Leverage fault exceptions in WCF to transmit user friendly error messages to the presentation layer when exceptions occur. Exceptions are errors that occur at runtime; exception handling is the technique of handling these runtime errors.

Which type of exception can be thrown from WCF service?

Expected exceptions from communication methods on a WCF client include TimeoutException , CommunicationException , and any derived class of CommunicationException . These indicate a problem during communication that can be safely handled by aborting the WCF client and reporting a communication failure.

Which contract is used for error handling in WCF?

Since a client's concern area is not about how an error occurred or the factors contributing to an error, SOAP Fault contract is used to communicate the error message from the service to the client in WCF. A Fault contract enables the client to have a documented view of the errors occurred in a service.


1 Answers

There are a few things you need to do to ensure exceptions bubble over HTTP pipe to the client .

  1. You must attribute your DataService class with the following:

    [ServiceBehavior(IncludeExceptionDetailInFaults = true)] public class MyDataService : DataService

  2. You must enable verbose errors in the configuration:

    public static void InitializeService(DataServiceConfiguration config) { config.UseVerboseErrors = true; }

It is best to throw DataServiceException within. The WCF Data Service runtime knows how to map the properties to the HTTP response and will always wrap it in a TargetInvocationException.

[WebGet]
public Entity OperationName(string id)
{
    try
    {
        //validate param
        Guid entityId;
        if (!Guid.TryParse(id, out entityId))
            throw new ArgumentException("Unable to parse to type Guid", "id");

        //operation code
    }
    catch (ArgumentException ex)
    {
        throw new DataServiceException(400, "Code", ex.Message, string.Empty, ex);
    }
}

You can then unpack this for the client consumer by overriding the HandleException in your DataService like so:

/// <summary>
/// Unpack exceptions to the consumer
/// </summary>
/// <param name="args"></param>
protected override void HandleException(HandleExceptionArgs args)
{
    if ((args.Exception is TargetInvocationException) && args.Exception.InnerException != null)
    {
        if (args.Exception.InnerException is DataServiceException)
            args.Exception = args.Exception.InnerException as DataServiceException;
        else
            args.Exception = new DataServiceException(400, args.Exception.InnerException.Message);
    }
}

See here for more info...

like image 51
jaimie Avatar answered Sep 17 '22 08:09

jaimie