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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10077121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:21:49  来源:igfitidea点击:

Datacontract exception. Cannot be serialized

c#.netwcf

提问by Justin

I have the following WCF DataContract:

我有以下 WCF DataContract:

[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. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute.

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

One of my methods is returning a Listof type Occupant. Would this be causing it?

我的方法之一是返回 aList类型Occupant。这会导致它吗?

采纳答案by StuartLC

Because you have provided one or more initializing constructors, you will also need to add a parameterless (default) constructor.

因为您已经提供了一个或多个初始化构造函数,所以您还需要添加一个无参数(默认)构造函数。

i.e. You need to add:

即您需要添加:

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

This is because the default constructor disappearswhen you add an explicit constructor.

这是因为当您添加显式构造函数时,默认构造函数会消失

[The issue isn't with the method returning List<Occupant>, since methods aren't serialized).]

[问题不在于方法返回List<Occupant>,因为方法未序列化)。]

回答by Rob Rodi

Try adding an empty constructor. Often times that will set off the serializer.

尝试添加一个空的构造函数。通常这会触发序列化程序。

回答by SwDevMan81

My guess would be because _Emailis not initialized. You could set EmitDefaultValueto falseand see if that helps:

我的猜测是因为_Email没有初始化。您可以设置EmitDefaultValuefalse,看看是否有帮助:

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

回答by rada

You should add an empty parameter constructor to your datacontract class

您应该向 datacontract 类添加一个空参数构造函数

回答by Zack Peterson

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

回答by Anton Semenov

When you have NO setter by default, do these steps:

默认情况下没有设置器时,请执行以下步骤:

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

This helps me.

这对我有帮助。