Click here to Skip to main content
15,912,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

I would like to ask direction on how to implement centralize handling of exception in web service? like I don't wanna write try catch in every web method instead I'll have one error event handler and whenever there's and exception it will go to that event handle and i can perform logging.

I have tried IErrorHandler but didn't work.
Ideas is very much appreciated.. thanks in advance..

Solution: Implement IErrorHandler
Edited: Once handled the exception, can we instruct the ErrorHandler not to continue on throwing exception?

-jeph-
Posted
Updated 8-Dec-11 5:46am
v3

Use the Application_Error event.

See here : http://msdn.microsoft.com/en-us/library/24395wz3.aspx[^]

[EDIT]
For Web Services see here : User Friendly ASP.NET Exception Handling[^]
 
Share this answer
 
v2
Comments
Jephunneh Malazarte 8-Dec-11 1:43am    
hello Mehdi. I am thinking that Global.asax will only work for the web application and not for web service?

because i tried using that and it won't go inside that handler even though i forcefully throw an exception.
Mehdi Gholam 8-Dec-11 1:48am    
I have updated the solution.
Jephunneh Malazarte 8-Dec-11 2:20am    
thanks i'll review the article and mark it accepted if it addressed my problem. seems like there's a need for me to change in my configuration however in my case i am using WCF Rest, i am not sure if it will make a huge difference though.
Monjurul Habib 8-Dec-11 15:42pm    
5!
Jephunneh Malazarte 8-Dec-11 20:24pm    
hi mehdi i have accepted the suggested solution as i am now working on the web pages. and a 5. :)
C#
public class ServiceHostGeneralErrorHandler : IErrorHandler
{
    public void ProvideFault(Exception ex, MessageVersion version, ref Message fault)
    {
        if (ex is FaultException)
            return;

        // a general message to the client
        var faultException = new FaultException("A General Error Occured");
        MessageFault messageFault = faultException.CreateMessageFault();
        fault = Message.CreateMessage(version, messageFault, null);
    }

    public bool HandleError(Exception ex)
    {
        // log the exception

        // mark as handled
        return true;
    }
}

Please mark as solution if it solved your problem.
 
Share this answer
 
Comments
Jephunneh Malazarte 8-Dec-11 2:10am    
is there any configuration that i need to setup in my web.config file?
Jephunneh Malazarte 8-Dec-11 2:13am    
this is what i did an hour ago but what i noticed here is that when i added a breakpoint in ProvideFault handler it won't stop there. I have also added <servicedebug includeexceptiondetailinfaults="true"> in my web.config too but still can't debug in that line of code.

am i not supposed to debug it when exception occur?
Jephunneh Malazarte 8-Dec-11 2:49am    
thanks the suggested solution leads me to the solution that solved my problem.
Jephunneh Malazarte 8-Dec-11 4:13am    
Sagar, any idea like after handling the exception normally it will still go through and disply the exception message right? but how can I implement where after i handled the exception "my web method" will simply return false if error else true?

based on my understanding is i still need to write try catch on every web method right?
sagar15modi 8-Dec-11 6:08am    
An interesting and useful WCF extensibility point for error handling:
WCF is a very extensible framework. You can explicitly control the behaviour of your application when an exception is thrown.
You can
- Decide to send a fault to the client or not,
- Replace an exception with a fault,
- Replace a fault with another fault,
- Perform logging,
- Perform other custom activities

In order to utilise this extensibility feature, you need to implement the IErrorHandler interface. You will then need to install your custom error handler by adding it to the ErrorHandlers property of the channel dispatchers for your service. It is possible to have more than one error handler and they are called in the order they are added to this collection.

for more detail follow the link
http://blogs.msdn.com/b/pedram/archive/2008/01/25/wcf-error-handling-and-some-best-practices.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900