gpt4 book ai didi

c# - DataContract 运行时错误 - 无法序列化类型 'myType'。我做错了什么?

转载 作者:行者123 更新时间:2023-12-02 22:24:02 24 4
gpt4 key购买 nike

尝试传递包含带有接口(interface)和接口(interface)/类列表的原始类型的“复杂”类类型。

我猜有问题的成员是:

public List<IMyInterface> IntrfList

运行时错误:

An error occurred while receiving the HTTP response to http:/localhost/xxxxxx/xxxxxService.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

来自 MS 服务跟踪查看器 (SvcTraceViewer) 的描述性错误:

Type 'myType' 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. See the Microsoft .NET Framework documentation for other supported types.

我按照解释建议做了,但没有帮助。

“共享”类:

[System.Runtime.Serialization.DataContract]
public class ServerState
{
private Queue<IJob> _mWaitingQueue;
public Queue<IJob> mWaitingQueue
{
get
{
lock (_LockObjWaiting)
{
return _mWaitingQueue;
}
}
private set
{
_mWaitingQueue = value;
}
}
private object _LockObjWaiting = new object();

private List<IJob> _mInPrograssList = new List<IJob>();

[DataMember]
public List<IJob> mInPrograssList
{
get
{
return _mInPrograssList;
}
private set
{
_mInPrograssList = value;
}
}
}

IJob 是一个接口(interface)。IJob的结构是这样的:

IJob (Interface)
- JobBase (Abstract class)
- JobA (Driven from JobBase)
- JobB (Driven from JobBase)

最佳答案

好的,知道了!

我在博客中发布了解决方案:http://livshitz.wordpress.com/2012/11/06/wcf-serialization-datacontract-runtime-error-type-mytype-cannot-be-serialized/

在与那个“共享”类的所有可能部分进行斗争和切割之后,我到达了有问题的区域。

问题是我将接口(interface)用作与驱动类相关联的成员(或列表..)。

就是这样!序列化程序在弄清楚如何序列化该成员时遇到问题,所以这里是解决方案:

在将要通过 WCF 序列化和共享的类中使用接口(interface)作为成员(或接口(interface)列表)时,Shared 类必须指定可能的通过添加接口(interface)类型:

[System.Runtime.Serialization.KnownType(typeof(JobA))]
[System.Runtime.Serialization.KnownType(typeof(JobB))]

并且每个类型都必须标有:

[System.Runtime.Serialization.DataContract]

注意:由于 JobBase 是抽象的,因此没有理由用 KnownType 标记它......

就是这样。

关于c# - DataContract 运行时错误 - 无法序列化类型 'myType'。我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13245917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com