2017-09-15 87 views
1

我面对的是一个设计/建筑问题,并不能找出一个干净的解决方案。我们有一个带有数百个WebMethods的.NET WebService。 (见下面的例子)。如果有任何所谓的WebMethods的抛出未处理的异常,我们要生成的电子邮件。.NET WebService的错误处理设计

问题:在这种情况下使用干净的设计模式是什么。我想避免将重复代码到几百方法呢?

[WebService(Namespace = "http://example.com/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[Serializable] 
public class WebService : System.Web.Services.WebService 
{ 
    [WebMethod] 
    public string GetQuoteInfo(string request) 
    { 
     return QuoteService.GetQuoteInfo(request); 
    } 

    [WebMethod] 
    public string GetQuoteAPR(string request) 
    { 
     return QuoteService.GetQuoteAPR(request); 
    } 

    [WebMethod] 
    public string GetAccountContactInfo(string request) 
    { 
     return AccountService.GetAccountContactInfo(request); 
    } 

    ... 
    ... 
    ... 

    [WebMethod] 
    public string GetAccountContactInfo(string request) 
    { 
     // implementation 
    } 

}

+1

可能相关的博客文章:应对全球Web服务未处理的异常(https://blogs.msdn.microsoft.com/nikhilsi/2008/06/11/handling-global-web-service-unhandled-exceptions/) 。 – Romoku

回答

0
  1. 介绍一个包装实际方法与正确的异常处理try/catch块类。

    public static class InvocationHelper 
    { 
        public static string SafeInvoke(Func<string, string> f, string request) 
        { 
         try 
         { 
          return f(request);  
         } 
         catch(Exception ex) 
         { 
          NofityAboutException(ex); 
          return null; 
         } 
        } 
    } 
    
  2. 使用类修改您的WebService方法:

    [WebMethod] 
    public string GetQuoteInfo(string request) 
    { 
        return InvocationHelper.SafeInvoke(r => QuoteService.GetQuoteInfo(r), request); 
    } 
    
0

尝试 {

你的代码在你这里Web方法

} 

    catch (Exception e) 
    { 

//电子邮件此错误消息

 e.Message; 
     throw; 
    } 

把它放在你所有的方法,有没有办法获得完美的错误消息。它的一次工作是获取错误信息,并且您可以随时自由地调试您的代码。

0

的一种方法是使用一个AOP库如NConcern。 根据您的情况可能有一定的局限性,因为图书馆必须CNeptune被改写(见NCover的例子)。