0

I have the following code in my ServiceBase abstract class:

    internal ServiceResponse ExecuteNonQuery(Action action)
    {
        try
        {
            action();
            return new ServiceResponse() { IsFaulty = false };
        }
        catch (DbEntityValidationException e)
        {
            OnDbEntityValidationException(e);
            return new ServiceResponse() { IsFaulty = true, Exception = e};
        }
        catch (Exception e)
        {
            _logger.Log(e);
            return new ServiceResponse() {IsFaulty = true, Exception = e};
        }
    }

All my services derives from this class and my WCF service endpoint looks like this:

    public ServiceResponse Add(Client client)
    {
        _logger.LogInformation($"ClientService.Add()");
        return ExecuteNonQuery(() =>
        {
            using (var context = _contextFactory.Build())
            {
                context.Clients.Add(_serviceToDalMapper.Map(client));
                context.SaveChanges();
            }
        });
    }

On the client side I have similar Try/Catch method when calling my service.

What I don't understand is that when an exception is thrown on the service side, the exception is catch, but I still get an System.ServiceModel.CommunicationException on the client side. Why ? I catched my exception, shouldn't my service just return the ServiceResponse?

Wilman Patel
  • 81
  • 10
Emmanuel Istace
  • 1,209
  • 2
  • 14
  • 32

2 Answers2

0

Add a try-catch inside your current catch, looks like the call is generating an exception, possibly generated inside the catch.

 catch (DbEntityValidationException e)
 {
      try
      {
        OnDbEntityValidationException(e);
        return new ServiceResponse() { IsFaulty = true, Excepetion = e};
      }
      catch
      {
        return new ServiceResponse() { IsFaulty = true, Excepetion = new Exception("Error handling the error")};   
      }
 }
Roman
  • 11,966
  • 10
  • 38
  • 47
Cleptus
  • 3,446
  • 4
  • 28
  • 34
  • Apparently WCF can't serialize Exception, that's the issue as I try to send the exception back to the client... thx – Emmanuel Istace Jan 27 '17 at 11:26
  • 1
    @EmmanuelIstace perhaps you should check for FaultContract http://stackoverflow.com/questions/5717231/faultexception-and-custom-exception-wcf – Cleptus Jan 27 '17 at 11:29
  • Thank's, when looking for the reason why I got the exception I found the FaultContract attribute, as it seems to be a best practice, I'll use that in the future :) – Emmanuel Istace Jan 27 '17 at 11:31
  • 1
    Using a FaultContract is of course good practice for exceptions you want the client to be able to handle. But for other, technical exceptions, you can let the exception propagate from your service method. You can also implement System.ServiceModel.Dispatcher.IErrorHandler (in particular the ProvideFault method) to wrap the exception in a more friendly FaultException. – Joe Jan 27 '17 at 14:45
0

WCF can't serialize the Exception object (which makes sense in a way).

So an exception was thrown in my catch block, that's the fault here.

According to WCF best practices, I should be using FaultContract to communicate exception to the client.

Emmanuel Istace
  • 1,209
  • 2
  • 14
  • 32