IncludeExceptionDetailInFaults doesn't behave like a thought

I have this simple test project to test the behavior of IncludeExceptionDetailInFaults.

public class Service1 : IService1 { public string GetData(int value) { throw new InvalidCastException("test"); return string.Format("You entered: {0}", value); } } [ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); } 

In app.config service I set to true

  <serviceDebug includeExceptionDetailInFaults="True" /> 

On the client side:

  try { using (var proxy = new ServiceReference1.Service1Client()) Console.WriteLine(proxy.GetData(5)); } catch (Exception ex) { Console.WriteLine(ex.Message); } 

Here's what I thought about what it was: Setting to enableExceptionDetailInFaults = true will propagate the exception details to the client. But I always get CommunicationObjectFaultException exception.

I tried using FaultContract (typeof (InvalidCastException)) in the contract, but the same behavior, only getting a CommunicationObjectFaultException.

The only way to make it work is to throw a new FaultException (new InvalidCastException ("test"));

But I thought that with IncludeExceptionDetailInFaults = true this was done automatically.

Am I missing something?

+4
source share
1 answer

This is because you installed the service client inside the using block.

WCF clients are one place in .NET where you should not use using because it will mask the "real" exception.

Technical explanation: Dispose raises Close , which will always raise a CommunicationObjectFaultedException if the channel already crashes (i.e. as a result of the previous exception), which subsequently places this exception on the top of the stack. When clearing ICommunicationObject , to avoid masking the exception, you should first check State to make sure it is faulty, and if so call Abort instead of Close .

+7
source

Source: https://habr.com/ru/post/1305772/


All Articles