Before reading this article, I highly recommend that you read the previous parts of this series:
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);
- }
- }
- }
Let's add one more class to our project and name it “
ErrorHandlerAttribute”. Now I will inherit this class from the “
Attribute” abstract class and implement it by the “
IServiceBehavior” interface.
- using System;
- using System.ServiceModel.Description;
- using System.ServiceModel.Dispatcher;
-
- namespace wcfService
- {
- public class ErrorHandlerAttribute : Attribute,IServiceBehavior
- {
- private readonly Type errorHandler;
- public ErrorHandlerAttribute(Type errorHandler)
- {
- this.errorHandler = errorHandler;
- }
- public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
- {
-
- }
-
- public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
- {
- IErrorHandler handler = (IErrorHandler)Activator.CreateInstance(this.errorHandler);
- foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
- {
- ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher;
- if (channelDispatcher != null) {
- channelDispatcher.ErrorHandlers.Add(handler);
- }
- }
- }
-
- public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
- {
-
- }
- }
- }
There is no need to provide any implementation of the Validate and AddBindingParameters methods as of now.
Now I will decorate the “
MathService” class with “
ErrorHandlerAttribute”.
In MathService.svc.cs
- using System;
-
- namespace wcfService
- {
- [ErrorHandlerAttribute(typeof(MyAppErrorHandler))]
- public class MathService : IMathService
- {
- public int AddTwoNo(int FirstNo, int Second)
- {
- return FirstNo + Second;
- }
-
-
- public int DivideTwoNo(int first, int second)
- {
- return first / second;
- }
- }
- }
Let's run the SQL query and see the output.
Now run the application and see the output. You can make a breakpoint on ProvideFault and HandleError for checking that the HandleError method works asynchronously or not.
When running the application it will throw an error.
Press F5 and continue to run the application and see the output.
Let's run the SQL query and see whether or not the output is the error saved in the database.
I hope you will enjoy this article.