Before reading this article, I highly recommend that you read the previous parts of this series:
- Introduction to WCF: Part 1
- Introduction to Endpoint in WCF: Part 2
- How to Make Changes to WCF Service Without Breaking Client in WCF: Part 3
- Method Overloading in WCF: Part 4
- What a DataContract and DataMember are in WCF: Part 5
- KnownType Attribute in WCF: Part 6
- MessageContract in WCF: Part 7
- Tracing in WCF: Part 8
- Exception handling in WCF: Part 9
I will use the same application as in my previous articles.
Let's add a class to the wcfService project and name it MyAppErrorHandler.cs and implement an IErrorHandler interface. The IErrorHandle interface is in the System.ServiceModel.Dispatcher namespace.
The IErrorHandler interface has the following two methods.
- HandleError: The return type of this method is bool and it is called asynchronously after providing the fault method.
- ProvideFault: The return type of this method is void. Whenever there is an unhandled exception, this method is called automatically. In this function will convert the .NET exception into a SOAP fault exception.
Let's understand the use of these functions. For this I will create a table and Stored Procedure.
I will create a table “Exception_Master” and a procedure “USP_INSERTEXCEPTION” for saving the exceptions in the database.
- CREATE TABLE [dbo].[Exception_Master](
- [ID] [int] IDENTITY(1,1) NOT NULL,
- [Message] [varchar](100) NULL,
- [Type] [varchar](50) NULL,
- [Source] [varchar](max) NULL,
- [CreatedDate] [date] NULL
- )
- CREATE PROCEDURE [DBO].[USP_INSERTEXCEPTION]
- (
- @Message [varchar](100) = NULL,
- @Type [varchar](50) = NULL,
- @Source [varchar](max) = NULL
- )
- AS
- BEGIN
- INSERT INTO Exception_Master(Message,Type,Source) VALUES(@Message,@Type,@Source)
- END
Now I will implement the IErrorHandle interface in the MyAppErrorHandler class.
MyAppErrorHandler.cs
- using System;
- using System.ServiceModel;
- using System.ServiceModel.Channels;
- using System.ServiceModel.Dispatcher;
- using System.Data;
- using System.Data.SqlClient;
- namespace wcfService
- {
- public class MyAppErrorHandler : IErrorHandler
- {
- SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Goods;Integrated Security=True;");
- public bool HandleError(Exception error)
- {
- string strSQL = string.Empty;
- bool status = true;
- con.Open();
- try
- {
- con.Open();
- SqlCommand cmd = new SqlCommand("USP_INSERTEXCEPTION",con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@Message", error.Message.ToString());
- cmd.Parameters.AddWithValue("@Type", error.GetType().Name.ToString());
- cmd.Parameters.AddWithValue("@Source", error.Source.ToString());
- cmd.ExecuteNonQuery();
- con.Close();
- }
- catch (Exception ex) {
- status = false;
- }
- return status;
- }
- public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
- {
- if (error is FaultException)
- return;
- FaultException FE = new FaultException("This is test Fault Exception");
- MessageFault MF = FE.CreateMessageFault();
- fault = Message.CreateMessage(version, MF, null);
- }
- }
- }

erdemPosted Mar 20, 2022, 7:04 PM
Thanks for the nice and useful article
Pankajkumar PatelPosted Sep 13, 2019, 12:55 AM
Nice article
veera muthuPosted Mar 11, 2016, 11:55 AM
good one....
Santhakumar MunuswamyPosted Jun 8, 2015, 3:03 PM
Thanks for nice article