By default, when an unhandled exception occurs in a WCF service, the service sends a SOAP Fault message across the wire to the client. The fault message normally contains no specific information as of why a certain operation failed. This is to prevent any tampering with the service by exposing too much fault information. The default fault message is…
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 3.0 SDK documentation and inspect the server trace logs.
For debugging purposes (not recommended for production) you can include full details in a SOAP Fault message. To do this enable includeExceptionDetailInFaults in the serviceDebug configuration element in the service App.config. For this example I use the EchoService WCF library.
<behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors>
In the EchoService Echo operation, whenever I want to echo the word “Fault”, an exception is thrown.
public EchoMessage Echo(EchoMessage message) { if(message.Text == "Fault") throw new Exception("Exception thrown!"); message.Invoked = DateTime.Now; message.Text = String.Concat( Enumerable.Repeat(message.Text, 3)); _echoList.Add(message); return message; }
On the client side we catch the Exception and simple write it to the console window. Now the “Exception thrown!” message is displayed.
using System; using EchoClientConsole.EchoServiceReference; namespace EchoClientConsole { internal class Program { private static void Main() { var channel = new EchoServiceClient( "WSHttpBinding_IEchoService" ); try { channel.Echo(new EchoMessage { Created = DateTime.Now, Text = "Fault" }); Console.ReadLine(); channel.Close(); } catch (Exception exception) { Console.WriteLine(exception.Message); Console.WriteLine(exception); Console.ReadLine(); channel.Abort(); } } } }
