如何:在WCF中集中处理异常 [英] How: Centralize Handling of Exception in WCF

查看:76
本文介绍了如何:在WCF中集中处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想问一下如何在Web服务中实现异常集中处理的方向?就像我不想在每个Web方法中编写try catch一样,相反,我将有一个错误事件处理程序,只要有异常,它将转到该事件句柄,并且我可以执行日志记录.

我尝试了IErrorHandler,但是没有用.
非常感谢您的想法..在此先感谢..

解决方案:实现IErrorHandler
一旦处理了异常,是否可以指示ErrorHandler不要继续引发异常?

-jeph-

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-

推荐答案

使用Application_Error事件.

参见此处: http://msdn.microsoft.com/en-us/library/24395wz3.aspx [^ ]


对于Web服务,请参见此处:用户友好的ASP.NET异常处理 [
Use the Application_Error event.

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


For Web Services see here : User Friendly ASP.NET Exception Handling[^]


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.


这篇关于如何:在WCF中集中处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆