1

WCFと名前付きパイプを使用する単純なIPCメカニズムがあります。私の目標は、ロギングの目的で例外の詳細(スタックトレースを含む)をクライアントに伝達することです(残りのアプリケーションロギングはクライアントにあります)。

次のコードを使用すると、クライアントでFaultException <Exception>をキャッチし、例外の詳細を確認できます。

契約:

[ServiceContract]
public interface IService
{
    [OperationContract]
    [FaultContract(typeof(Exception))]
    void DoSomething();
}

実装:

public class Service : IService
{
    public void DoSomething()
    {
        try
        {
            ThisWillThrowAnException();
        }
        catch (Exception e)
        {
            throw new FaultException<Exception>(e);
        }
    }
 }

クライアント:

public void CallServer()
{
    try
    {
        proxy.DoSomething();
    }
    catch (FaultException<Exception> e)
    {
        Console.WriteLine("Caught fault exception!");
    }
}

これは正常に機能し、コンソールにメッセージが印刷されます。ただし、基本のExceptionクラスの代わりに独自の派生例外を使用したい場合は、失敗します。

カスタム例外:

[Serializable]
public class MyException : Exception
{
    public MyException () { }
    public MyException (string message) : base(message) { }
    public MyException (string message, Exception inner) : base(message, inner) { }
    protected MyException (
      SerializationInfo info,
      StreamingContext context)
        : base(info, context) { }
}

IService.DoSomethingのFaultContractをに変更します

typeof(MyException).

Serviceのthrow句を次のように変更します

new FaultException<MyException>(new MyException(e.Message, e);

クライアントのcatch句を次のように変更します

catch (FaultException<MyException> e)

これを実行すると、CommunicationExceptionがクライアントで次のエラーでキャッチされます: System.ServiceModel.CommunicationException:パイプからの読み取り中にエラーが発生しました:パイプは終了しました。(109、0x6d)。

MyExceptionクラスは、クライアントとサーバーの両方で使用できる共有ライブラリにあります。

この質問はこの質問と非常に似ていますが、それは私を助けませんでした。

4

1 に答える 1

2

StackFrameのシリアル化されたリストを含む独自の障害DataContractを作成することで、これを解決しました。

どうやらこのMSDNの記事は正確ではありませんか?

http://msdn.microsoft.com/en-us/library/ff649840.aspx

[DataContract]
public class MyFault
{
    [DataMember]
    public string Message { get; set; }

    [DataMember]
    public IList<SerializableMiniStackFrame> StackTrace { get; set; }


    public static MyFault CreateFault(Exception e)
    {
        MyFault fault = new MyFault();
        fault.Message = e.Message;
        fault.InitTrace(e);
        return fault;
    }

    /// <summary>
    /// Initializes the stack trace based on when the inner exception was thrown.
    /// </summary>
    /// <param name="inner">The inner exception.</param>
    private void InitTrace(Exception inner)
    {
        StackTrace trace = new StackTrace(inner, true);
        InitTrace(trace);
    }

    /// <summary>
    /// Initializes the internal serializable stack frames based on the given
    /// stack trace.
    /// </summary>
    /// <param name="stackTrace">The stack trace.</param>
    private void InitTrace(StackTrace stackTrace)
    {
        // Create a new list of serializable frames.
        this.StackTrace = new List<SerializableMiniStackFrame>();
        // Iterate over each frame in the stack trace.
        foreach (StackFrame frame in stackTrace.GetFrames())
        {
            string type = "";
            Type declaringType = frame.GetMethod().DeclaringType;
            if (null != declaringType)
            {
                type = declaringType.FullName;
            }

            MethodBase method = frame.GetMethod();
            string methodName = method.Name;
            string parameters = string.Empty;
            string delimiter = string.Empty;
            foreach (ParameterInfo parameter in method.GetParameters())
            {
                parameters += string.Format("{0}{1} {2}", delimiter, parameter.ParameterType.Name, parameter.Name);
                delimiter = ", ";
            }
            string file = Path.GetFileName(frame.GetFileName());
            int line = frame.GetFileLineNumber();

            // Create a serializable frame and add it to the list.
            SerializableMiniStackFrame miniFrame = new SerializableMiniStackFrame(type, methodName, parameters, file, line);
            this.StackTrace.Add(miniFrame);
        }
    }
}

/// <summary>
/// This class encapsulates basic stack frame information into a serializable
/// object.
/// </summary>
[DataContract]
public class SerializableMiniStackFrame
{
    public SerializableMiniStackFrame() { }
    public SerializableMiniStackFrame(string type, string method, string parameters, string file, int line)
    {
        this.Type = type;
        this.Method = method;
        this.Parameters = parameters;
        this.File = file;
        this.Line = line;
    }

    [DataMember]
    public string Type { get; set; }
    [DataMember]
    public string Method { get; set; }
    [DataMember]
    public string Parameters { get; set; }
    [DataMember]
    public string File { get; set; }
    [DataMember]
    public int Line { get; set; }
}
于 2011-02-04T18:43:54.527 に答える