Lowy の ServiceModelEx から適用されたコードを使用して、例外を a 内にパッケージ化し、FaultException
それをネットワーク経由で送信してから、クライアント上で .NET としてアンパックしますException
。SqlException
私のサーバーは、理論的にはあらゆる種類の例外をスローできますが、例としてa を使用しましょう。
Lowy はリフレクションを使用してアンパックするため、コードは例外の型を調べて、有効なオブジェクトを構築できるかどうかを判断する必要があります。
static Exception ExtractException(ExceptionDetail detail)
{
Exception innerException = null;
if (detail.InnerException != null)
{
innerException = ExtractException(detail.InnerException);
}
Type type = Type.GetType(detail.Type);
Debug.Assert(type != null, "Make sure this assembly contains the definition of the custom exception");
Debug.Assert(type.IsSubclassOf(typeof(Exception)));
//...
}
2 つの質問
SqlExceptions では、最初の Debug.Assert が失敗します。それらをクライアントで解決しようとして、への参照を設定しました
System.Data
が、うまくいきません。System.Data
これは、そのクライアントで実際の呼び出しがなくても、コンパイラが参照を削除するためだと思いますか? 短期的には、クライアントにその.GetType
電話を解決してもらうために何ができますか?詳細の粒度 (例) を失うことなく、クライアント側でアセンブリ参照を必要とせず
LambdaException
に、をベースとして「再スロー」する正しい方法は何ですか? これは可能ですか?ExceptionDetail
StackTrace
EDIT : これは、サーバー上でをラップExceptionDetail
するソースです。FaultException
繰り返しますが、これは主に Lowy の ServiceModelEx からのものです。
public static void PromoteException(Type serviceType, Exception error, MessageVersion version, ref Message fault)
{
//Is error in the form of FaultException<T> ?
if (error.GetType().IsGenericType && error is FaultException)
{
Debug.Assert(error.GetType().GetGenericTypeDefinition() == typeof(FaultException<>));
return;
}
ExceptionDetail exdetail = new ExceptionDetail(error);
FaultException<ExceptionDetail> faultException = new FaultException<ExceptionDetail>(exdetail);
MessageFault messageFault = faultException.CreateMessageFault();
fault = Message.CreateMessage(version, messageFault, faultException.Action);
//...
}