Leverage fault exceptions in WCF to transmit user friendly error messages to the presentation layer when exceptions occur Exceptions are errors that occur at runtime; exception handling is the technique of handling these runtime errors. You would typically use try, catch, and finally blocks (also known as exception blocks) in your application’s code to handle exceptions. If exceptions are not handled properly in the application’s code and an exception has occurred at runtime, the execution of the application would terminate. Exception handling in WCF is not that straight forward – you are constrained to sending .Net objects over the wire and your WCF service can only send serialized data, i.e., SOAP messages to the client. You can handle exceptions in WCF in one of these three ways: Using FaultException Using IErrorHandler Using returnUnknownExceptionsAsFaults In this post, I will present a discussion on the various ways in which exception messages can be transmitted from the WCF service to the consumers of the service. Consider this simple WCF service. [ServiceContract] public interface IDBManagerService { [OperationContract] void Save(Employee emp); } The IDBManagerService service contract contains one operation contract to persist an employee object to the database. public class DBManagerService : IDBManagerService { void Save(Employee emp) { try { //Code to store an employee object to the database } catch(Exception ex) { throw new Exception(“Error occurred while saving data…”); } } } Now suppose there is an error connecting to the database or storing the employee object to the database at the time when you are trying to consume the service. You would then get an exception with this message: “System.ServiceModel.FaultException: 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.” You can use set the includeExceptionDetailInFaults element to true in the web.config file so that the additional details of the exception are included in the fault to make it more convenient to you to inspect what actually went wrong. You can also achieve this by writing code. Here is a code snippet that illustrates how you can set this property to true. typeof(ServiceDebugBehavior)); new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true }); You can also set this to true using the ServiceBehavior tag as shown below. [ServiceBehavior(IncludeExceptionDetailInFaults = true)] public class DBManagerService : IDBManagerService { } When you try to consume the service again, you would see a more precise exception message. Using FaultException However, if you need to pass user friendly exception messages from the service, you should throw fault exceptions. Fault exceptions are exceptions that are thrown by a WCF service when an exception occurs at runtime — such exceptions are typically used to transmit untyped fault data to the service consumers. You can handle exceptions in your service methods much the same way you do with other methods and then turn them into fault exceptions. The code snippet below shows the updated service method — the service method now throws fault exception. public class DBManagerService : IDBManagerService { void Save(Employee emp) { try { //Code to store an employee object to the database } catch(Exception ex) { throw new FaultException(“Error occurred while saving data…”); } } } You would now need to handle fault exception in your code when consuming this service. You can learn more on fault exceptions in WCF from this MSDN article. You can also create a custom fault class that is marked with the DataContract attribute. [DataContract] public class CustomFault { [DataMember] public string Source; [DataMember] public string ExceptionMessage; [DataMember] public string InnerException; [DataMember] public string StackTrace; } The following code snippet illustrates how you can use the CustomFault class to throw strongly-typed FaultException. void Save(Employee emp) { try { //Code to save the employee object into the database } catch (Exception ex) { CustomFault cx = new CustomFault(); throw new FaultException(ex, new FaultReason(“This is a strongly typed faulted exception”)); } } You would also need to specify the FaultContract attribute on your service method that would raise FaultException. The modified Save method would look like this. [ServiceContract] public interface IDBManagerService { [OperationContract] [FaultContract] void Save(Employee emp); } Using returnUnknownExceptionsAsFaults You can use the returnUnknownExceptionsAsFaults attribute in the service behavior configuration to raise an exception as a SOAP fault automatically. The following code snippet illustrates how you can achieve this. returnUnknownExceptionsAsFaults=”True”> Handling exceptions globally Another way to handle exceptions in WCF is by implementing the IErrorHandler interface on your service class to handle all exceptions globally and provide a SOAP compliant FaultException. This interface contains two methods — HandleError and ProvideFault. While the former is used to perform some activity with the error, the latter is used to return a fault message. Note that you can also configure IErrorHandler (turn it on or off) in your service configurable file. Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos