December 25, 2018

WCF - How to turn on IncludeExceptionDetailInFaults

You may encounter this error message while working with WCF services:

The server was unable to process the request due to an internal error.  For more information about the error, either turn on IncludeExceptionDetailInFaults
 (either from ServiceBehaviorAttribute or from the  configuration behavior) on the server in order to send the exception information back to the client, 
or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.

This error message is not showing the real error or exception cause on server side because of security reasons, but instructing to change the configuration in order to receive real exception message, which may be some internal server error.

To fix this error. We have two ways to set this flag's value, either by web.config file or from C# Code.

  1. Open the web.config file of the service. Define a behavior in your web.config file (it may be already defined by default, but with false value. e.g. includeExceptionDetailInFaults="false"). We need to set includeExceptionDetailInFaults="true" in order to include exception details:

    <configuration>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="debug">
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        ...
      </system.serviceModel>
    </configuration>
    

    Then apply or attach the behavior to your service:

    <configuration>
      <system.serviceModel>
        ...
        <services>
          <service name="MyServiceName" behaviorConfiguration="debug" />
        </services>
      </system.serviceModel>
    </configuration>
    
  2. You can also set the value of includeExceptionDetailInFaults from C# Code, by using ServiceBehavior attribute on the service's class declaration.

    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class MyServiceClass:IMyService
    {
     //my service methods...
    }
    

No comments:

Post a Comment