Exception handling with WCF Data Services
Asked Answered
B

4

9

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?

Basildon answered 17/8, 2010 at 10:58 Comment(0)
S
11

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...

Strophanthus answered 19/10, 2011 at 10:37 Comment(0)
Z
3

You can decorate your service class with this attribute ServiceBehaviorAttribute like so :

 [ServiceBehavior(IncludeExceptionDetailInFaults=true)]
 public class PricingDataService : DataService<ObjectContext>, IDisposable
 {
   ...
 }
Zayas answered 2/4, 2011 at 15:23 Comment(0)
A
0

You need to create custom exceptions for this. Please read this post here: Why Create Custom Exceptions?

Which language are you developing in?

If you need further guidance, please add some comments.

Amelita answered 17/8, 2010 at 11:4 Comment(1)
I am developing in C#. However, the exceptions I throw from the service does not reach the clients consuming the service.Basildon
S
0

I don't think he wants to know how to throw / catch exceptions in .NET.

He probably want to get thoughts on how to tell the clients consuming a WCF Data Service that something (and what) went wrong when an exception is being thrown / caught at the server(service) side.

WCF Data Services uses HTTP request / response messages and you can't just throw an exception from the service to the client.

Senna answered 17/8, 2010 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.