2

我正在尝试通过 NamedPipe 传递 Exception 类型的对象(或其子类之一)。

服务合同:

[ServiceContract]
public interface IWCFCallback
{
    [OperationContract]
    void sendException(Exception e);
}

当我像这样使用它时它工作正常:

_pipeproxy.sendException(new Exception("bla bla 99"));

但是一旦我通过了一个子类:

_pipeproxy.sendException(new ArgumentException("fridgemaster 3000"));

我得到一个异常,说反序列化失败。

我已经阅读了有关 KnownTypes 属性的信息,但我不知道如何将它用于我自己未实现的类。

有人可以在这里给我一个提示吗?

4

2 回答 2

2

WCF 的“最佳实践”之一是不要序列化异常。
如果您的 ServiceHost 正在抛出异常,那么您应该使用 FaultException。
异常不能安全传输的原因之一是 Exception 本身是可序列化的,但您可以从中派生,并且谁保证您的自定义派生异常将是可序列化的。

作为一种解决方法,您可以将异常堆栈作为字符串和类型作为枚举传递数据协定对象。

于 2013-08-09T08:59:47.700 回答
1

这可能不符合最佳实践,但您可以创建一个代表异常的 DataContract - 如下所示:

/// <summary>
/// Represents errors that occur during application execution.
/// </summary>
[DataContract]
public class ExceptionInfo
{
    /// <summary>
    /// Gets the type of the exception.
    /// </summary>
    [DataMember]
    public string ExceptionType
    {
        get;
        set;
    }

    /// <summary>
    /// Gets a message that describes the current exception.
    /// </summary>
    /// <returns>
    /// The error message that explains the reason for the exception, or an empty string("").
    /// </returns>
    [DataMember]
    public string Message
    {
        get;
        set;
    }

    /// <summary>
    /// Gets the <see cref="T:System.Exception"/> instance that caused the current exception.
    /// </summary>
    /// <returns>
    /// An instance of Exception that describes the error that caused the current exception. The InnerException property returns the same value as was passed into the constructor, or a null reference (Nothing in Visual Basic) if the inner exception value was not supplied to the constructor. This property is read-only.
    /// </returns>
    [DataMember]
    public ExceptionInfo InnerException
    {
        get;
        set;
    }

    /// <summary>
    /// Gets a string representation of the immediate frames on the call stack.
    /// </summary>
    /// <returns>
    /// A string that describes the immediate frames of the call stack.
    /// </returns>
    [DataMember]
    public string StackTrace
    {
        get;
        set;
    }

    /// <summary>
    /// Gets or sets a link to the help file associated with this exception.
    /// </summary>
    /// <returns>
    /// The Uniform Resource Name (URN) or Uniform Resource Locator (URL).
    /// </returns>
    [DataMember]
    public string HelpLink
    {
        get;
        set;
    }

    /// <summary>
    /// Gets or sets the name of the application or the object that causes the error.
    /// </summary>
    [DataMember]
    public string Source
    {
        get;
        set;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="ExceptionInfo"/> class.
    /// </summary>
    /// <param name="exception">The exception.</param>
    /// <exception cref="ArgumentNullException">exception</exception>
    public ExceptionInfo(Exception exception)
    {
        if(exception == null)
            throw new ArgumentNullException("exception");

        ExceptionType = exception.GetType().FullName;
        HelpLink = exception.HelpLink;
        Message = exception.Message;
        Source = exception.Source;
        StackTrace = exception.StackTrace;

        if(exception.InnerException != null)
        {
            InnerException = new ExceptionInfo(exception.InnerException);
        }
    }
}

服务合同:

[ServiceContract]
public interface IWCFCallback
{
    [OperationContract]
    void sendException(ExceptionInfo e);
}

用法:

try
{
    // .....
}
catch (Exception ex)
{
    var info = new ExceptionInfo(ex);

    // do something....
}
于 2013-08-09T09:21:25.980 回答