2017-09-15 81 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的例子)。