c# - 序列化用于 WCF 服务的 CLR 对象时出错

标签 c# wcf

我已经编写了一个自定义异常对象。这样做的原因是我想在发生错误时跟踪附加信息。我的 CLR 对象定义如下:

public class MyException : Exception
{
  public override string StackTrace
  {
    get { return base.StackTrace; }
  }
  private readonly string stackTrace;

  public override string Message
  {
    get { return base.Message; }
  }
  private readonly string message;

  public string Element
  {
    get { return element; }
  }
  private readonly string element;

  public string ErrorType
  {
    get { return errorType; }
  }
  private readonly string errorType;

  public string Misc
  {
    get { return misc; }
  }
  private readonly string misc;

  #endregion Properties

  #region Constructors

  public MyException() 
  {}

  public MyException(string message) : base(message)
  { }

  public MyException(string message, Exception inner) : base(message, inner)
  { }

  public MyException(string message, string stackTrace) : base()
  {
    this.message = message;
    this.stackTrace = stackTrace;
  }

  public MyException(string message, string stackTrace, string element, string errorType, string misc) : base()
  {
    this.message = message;
    this.stackTrace = stackTrace;
    this.element = element;
    this.errorType = errorType;
    this.misc = misc;
  }   

  protected MyException(SerializationInfo info, StreamingContext context) : base(info, context)
  {
    element = info.GetString("element");
    errorType = info.GetString("errorType");
    misc = info.GetString("misc");
  }

  public override void GetObjectData(SerializationInfo info, StreamingContext context)
  {
    base.GetObjectData(info, context);
    info.AddValue("element", element);
    info.AddValue("errorType", errorType);
    info.AddValue("misc", misc);
  }
}

我已经在 WP7 应用程序中创建了这个自定义 xception 的副本。唯一的区别是,我没有定义 GetObjectData 方法或定义了 SerializationInfo 的构造函数。如果我按原样运行应用程序,我会收到一条错误消息:

类型“My.MyException”无法序列化。考虑使用 DataContractAttribute 特性对其进行标记,并使用 DataMemberAttribute 特性对其所有要序列化的成员进行标记。如果类型是集合,请考虑使用 CollectionDataContractAttribute 对其进行标记。

如果我将 DataContract/DataMember 属性添加到类及其在服务器端的适当成员,我会收到一条错误消息: 类型不能是 ISerializable 且具有 DataContractAttribute 属性。

如何序列化 MyException 以便我可以将它的一个实例传递给我的 WCF 服务。请注意,我想通过 Android 应用程序使用我的服务。因此,我不想做任何过于以微软为中心的事情。这是我对 DataContract/DataMember 东西的恐惧。

非常感谢您的帮助!

最佳答案

您需要将[Serializable] 添加到您的自定义类中。这就是您的第一个错误最有可能告诉您的,尽管它告诉您使用首选的 WCF(使用 DataContract)方式,但它需要可序列化。

您不能混合使用序列化接口(interface)(DataContract 和 ISerializable),这是第二个错误告诉您的。 Exception 已经实现了 ISerializable,WCF 将支持它。从您的代码看来,您也已经为此添加了一些方法(带有 SerializationInfo 的方法)。

正如 Marc 所说,使用 DataContract 的东西并没有太以 .NET 为中心,它在客户端消费方面与平台无关。

关于c# - 序列化用于 WCF 服务的 CLR 对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4907662/

相关文章:

wcf - 分层架构和 Entity Framework 的类命名约定

c# - 由于 Base64 错误,无法将图像上传到数据库

c# - 从类库转换为 WCF 需要采取哪些步骤?

wcf - 使用 PowerShell 脚本在 IIS 上构建和部署 WCF 服务的步骤

c# - 如何在 C# 中创建嵌入式 Neo4j 数据库

c# - 如何创建表达式树以执行与 "StartsWith"相同的操作

c# - MatrixTransform 的矩阵在我看来是转置的

c# - Visual Studio 调试、跳转到方法末尾或调用方法

wcf - 从 WCF 中的 MEX/WSDL 隐藏 REST 端点

c# - Azure CloudBlockBlob。 DownloadToStream 时找不到 blob。 Uri 似乎重复了?