Search code examples
c#wcffaultexception

FaultException not thrown at WCF client. Instead I am getting Communication Exception


I have an simple WCF server where I am throwing FaultException<InvalidOperationException> but on client side i only get the Communication exception with error below.

The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:00:59.8080000'.

See below for code I am having, please help and let me know if something is missing.

Service

[OperationContract]
[FaultContract(typeof(InvalidOperationException))]
bool DoWork()

Service implementation code

public bool DoWork()
    {var invalidOperationException =
          new InvalidOperationException(
            "Can't dowork", exception);

        // throw InvalidOperationException as an FaultException to the service consumer.
        throw new FaultException<InvalidOperationException>(
          invalidOperationException,
             exception.Message,
              new FaultCode("Test Message"),
              "DoWork");
}

Client side code.

try
{
 var service = new ServiceClient();
 var result = service.DoWork();
}
catch (TimeoutException timeoutException)
          {

          }
          catch (FaultException<InvalidOperationException> invalidOperationException)
          {

          }
          catch (FaultException unknownFault)
          {

          }
          catch (CommunicationException communicationException)
          {

          }

Solution

  • The problem is the inner exception provided in

    var invalidOperationException =
          new InvalidOperationException(
            "Can't dowork", exception);
    

    If you remove that it will work.

    My guess is that It has something to do with trying to serialize an CLR exception.