[英]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
.我的方法之一是返回类型为Occupant
的List
。 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 时,请执行以下步骤:
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.