2010-12-01 14 views
1

Silverlightでは、私のサービス(この例ではWCF Data Service)からデータを取得するためにlambdaを使用します。私がtry catchでそれらを処理しない限り、コールバック内の例外はシステムによって飲み込まれます。たとえば、次のようにSilverlightで例外を処理する非同期ラムダコール

this.Context.BeginSaveChanges(() => 
{ 
    // throwing an exception is lost and the Application_UnhandledException doesn't catch it 

}, null); 

私は例外をログに記録し、ASPX一般的なエラーページにリダイレクトするヘルパー関数がありますが、私がしなければならないならば、私は、okですのtry /キャッチとラムダですべてをラップする必要がありそれは良い方法ですか?

答えて

2

あなたがして、ラムダのをラップするヘルパーメソッドのセットを作成することができます: -

public static class Helper 
{ 
    public static AsyncCallback GetAsyncCallback(Action<IAsyncResult> inner) 
    { 
     return (a) => 
     { 
      try 
      { 
       inner(a); 
      } 
      catch (Exception err) 
      { 
       // Your handling for "uncaught" errors 
      } 
     }; 
    } 

    public static Action GetAction(Action inner) 
     { 
     return() => 
     { 
      try 
      { 
       inner(); 
      } 
      catch (Exception err) 
      { 
       // Your handling for "uncaught" errors 
      } 
     }; 
     } 

     public static Action<T> GetAction(Action<T> inner) 
     { 
     return (a) => 
     { 
      try 
      { 
       inner(a); 
      } 
      catch (Exception err) 
      { 
       // Your handling for "uncaught" errors 
      } 
     }; 
     } 
     // and so on also:- 

     public static Func<T> GetFunc(Func<T> inner;) 
     { 
     return() => 
     { 
      try 
      { 
       return inner(); 
      } 
      catch (Exception err) 
      { 
       // Your handling for "uncaught" errors 
      } 
     }; 
     } 
     public static Func<T1, TReturn> GetFunc(Func<T1, TReturn> inner;) 
     { 
     return (a) => 
     { 
      try 
      { 
       return inner(a); 
      } 
      catch (Exception err) 
      { 
       // Your handling for "uncaught" errors 
      } 
     }; 
     } 
} 

今、あなたは、デフォルトの定型例外処理を気にすることなく、ラムダのをラップすることができます: -

this.Context.BeginSaveChanges(Helper.GetAsyncCallback((ar) =>    
{    
    // Only needs specific exception handling or none at all    

}), null);    
関連する問題