简体   繁体   English

数据合同异常。 无法序列化

[英]Datacontract exception. Cannot be serialized

I have the following WCF DataContract:我有以下 WCF 数据合同:

[DataContract]
public class Occupant
{
    private string _Name;
    private string _Email;
    private string _Organization;
    private string _Badge;

    public Occupant(string name, string badge, string organization)
    {
        Name = name;
        Badge = badge;
        Organization = organization;
    }

    public Occupant(string name, string badge)
    {
        Value = name;
        Key = badge;
    }

    [DataMember]
    public string Key
    {
        get { return _Name; }
        set { _Name = value; }
    }

    [DataMember]
    public string Value
    {
        get { return _Badge; }
        set { _Badge = value; }
    }

    [DataMember]
    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

    [DataMember]
    public string Email
    {
        get { return _Email; }
        set { _Email = value; }
    }

    [DataMember]
    public string Organization
    {
        get { return _Organization; }
        set { _Organization = value; }
    }

    [DataMember]
    public string Badge
    {
        get { return _Badge; }
        set { _Badge = value; }
    }
}

When I try to access this service via web browser (it is hosted on IIS), I am getting this error:当我尝试通过 web 浏览器(它托管在 IIS 上)访问此服务时,出现此错误:

System.Runtime.Serialization.InvalidDataContractException: Type 'MyNamespace.Occupant' cannot be serialized. System.Runtime.Serialization.InvalidDataContractException:无法序列化类型“MyNamespace.Occupant”。 Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.考虑使用 DataContractAttribute 特性对其进行标记,并使用 DataMemberAttribute 特性对其所有要序列化的成员进行标记。 If the type is a collection, consider marking it with the CollectionDataContractAttribute.如果类型是集合,请考虑使用 CollectionDataContractAttribute 对其进行标记。

One of my methods is returning a List of type Occupant .我的方法之一是返回类型为OccupantList Would this be causing it?这会导致它吗?

Because you have provided one or more initializing constructors, you will also need to add a parameterless (default) constructor.因为您已经提供了一个或多个初始化构造函数,所以您还需要添加一个无参数(默认)构造函数。

ie You need to add:即你需要添加:

[DataContract]
public class Occupant
{
    // *** Needed only for Serialization
    public Occupant() {}
    ...

This is because the default constructor disappears when you add an explicit constructor.这是因为当您添加显式构造函数时, 默认构造函数消失了。

[The issue isn't with the method returning List<Occupant> , since methods aren't serialized).] [问题不在于返回List<Occupant>的方法,因为方法没有序列化。]

Try adding an empty constructor.尝试添加一个空的构造函数。 Often times that will set off the serializer.通常这会触发序列化程序。

You need a default parameterless constructor.您需要一个默认的无参数构造函数。 I don't ever plan to actually use mine, so I added a summary for IntelliSense and throw a run-time exception to keep it from being used.我从未打算实际使用我的,因此我添加了 IntelliSense 的摘要并抛出运行时异常以防止它被使用。

/// <summary>
/// parameterless default constructor only for serialization
/// </summary>
public MyClass() { throw new NotImplementedException("parameterless default constructor only for serialization"); }

You should add an empty parameter constructor to your datacontract class您应该向数据合同添加一个空参数构造函数 class

When you have NO setter by default, do these steps:默认情况下没有 setter 时,请执行以下步骤:

  1. Add parametreless constructor;添加无参数构造函数;
  2. And add private set at least.并至少添加私有集

This helps me.这对我有帮助。

My guess would be because _Email is not initialized.我的猜测是因为_Email没有初始化。 You could set EmitDefaultValue to false and see if that helps:您可以将EmitDefaultValue设置为false并查看是否有帮助:

[DataMember(EmitDefaultValue = false)]
public string Email
{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用WCF DataContract的InvalidDataContractException(“类型无法序列化”)错误 - InvalidDataContractException (“Type cannot be serialized”) error using WCF DataContract DataContract运行时错误-类型&#39;myType&#39;无法序列化。 我做错了什么? - DataContract runtime error - Type 'myType' cannot be serialized. What i'm doing wrong? SqlParameterCollection异常。 NHibernate - SqlParameterCollection exception. NHibernate XPath匹配XML序列化的DataContract类 - XPath matches for a XML serialized DataContract class 数据合同的序列化异常 - serialization exception with datacontract 溢出异常。 StringBuilder的 - Overflow Exception. StringBuilder DataContract包含Exception - DataContract which includes Exception Windows Phone上的HTTPClient“无法序列化RuntimeClass类型”异常 - “Type RuntimeClass cannot be serialized” exception with HTTPClient on Windows Phone &#39;Protexis。׋&#39;的类型初始值设定项引发了异常。 值不能为空。 参数名称:path1 - The type initializer for 'Protexis.׋' threw an exception. Value cannot be null. Parameter name: path1 发生内部异常:类型 &#39;&lt;&gt;f__AnonymousType2`6[...]&#39; 无法序列化 - Internal exception has occured: Type '<>f__AnonymousType2`6[...]' cannot be serialized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM