如何:在WCF中集中处理异常 [英] How: Centralize Handling of Exception in 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 theApplication_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屋!