1

Lowy の ServiceModelEx から適用されたコードを使用して、例外を a 内にパッケージ化し、FaultExceptionそれをネットワーク経由で送信してから、クライアント上で .NET としてアンパックしますExceptionSqlException私のサーバーは、理論的にはあらゆる種類の例外をスローできますが、例として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 つの質問

  1. SqlExceptions では、最初の Debug.Assert が失敗します。それらをクライアントで解決しようとして、への参照を設定しましたSystem.Dataが、うまくいきません。System.Dataこれは、そのクライアントで実際の呼び出しがなくても、コンパイラが参照を削除するためだと思いますか? 短期的には、クライアントにその.GetType電話を解決してもらうために何ができますか?

  2. 詳細の粒度 (例) を失うことなく、クライアント側でアセンブリ参照を必要とせずLambdaExceptionに、をベースとして「再スロー」する正しい方法は何ですか? これは可能ですか?ExceptionDetailStackTrace

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);

        //...
    }
4

0 に答える 0