[英]DataContract runtime error - Type 'myType' cannot be serialized. What i'm doing wrong?
Trying to pass "complex" class type that contains primitive types with interfaces and lists of interfaces/classes. 试图传递“复杂”类类型,该类类型包含具有接口的原始类型以及接口/类的列表。
I guess the problematic member is: 我猜有问题的成员是:
public List<IMyInterface> IntrfList
Runtime Error: 运行时错误:
An error occurred while receiving the HTTP response to http:/localhost/xxxxxx/xxxxxService.svc. 收到对http:/localhost/xxxxxx/xxxxxService.svc的HTTP响应时发生错误。 This could be due to the service endpoint binding not using the HTTP protocol. 这可能是由于服务端点绑定未使用HTTP协议。 This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). 这也可能是由于服务器终止了HTTP请求上下文(可能是由于服务关闭了)。 See server logs for more details. 有关更多详细信息,请参见服务器日志。
The descriptive error from MS Service Trace Viewer (SvcTraceViewer): 来自MS Service跟踪查看器(SvcTraceViewer)的描述性错误:
Type 'myType' cannot be serialized. 类型“ myType”无法序列化。 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对其进行标记。 See the Microsoft .NET Framework documentation for other supported types. 有关其他受支持的类型,请参见Microsoft .NET Framework文档。
I did as the explanation suggests but didn't help. 我按照解释的建议做了,但是没有帮助。
The "shared" class: “共享”类:
[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 is an interface. IJob是一个接口。 The structure of IJob is like this: IJob的结构如下:
IJob (Interface)
- JobBase (Abstract class)
- JobA (Driven from JobBase)
- JobB (Driven from JobBase)
Alright, got it! 好吧,知道了!
I posted the solution in my blog: http://livshitz.wordpress.com/2012/11/06/wcf-serialization-datacontract-runtime-error-type-mytype-cannot-be-serialized/ 我将解决方案发布在了我的博客中: http : //livshitz.wordpress.com/2012/11/06/wcf-serialization-datacontract-runtime-error-type-mytype-cannot-be-serialized/
After fighting and cutting all possible parts of that “shared” class, I got to the problematic area. 在与“共享”班级的所有可能组成部分进行战斗和削减之后,我进入了有问题的领域。
The problem was that I used interface as a member (or list of..), which associated with Driven classes. 问题是我将interface用作与Driven类相关联的成员(或..的列表)。
And that's it! 就是这样! The serializer having problem figuring out how to serialize that member, so here is the solution: 序列化器在弄清楚如何序列化该成员时遇到问题,因此这是解决方案:
When using interface as a member (or list of interface) in a class that is about to be serialized and shared via WCF, the Shared class must specify the possible types of the interface by adding to it: 当使用interface作为要通过WCF进行序列化和共享的类的成员(或接口列表)时, Shared类必须通过添加接口来指定接口的可能类型:
[System.Runtime.Serialization.KnownType(typeof(JobA))]
[System.Runtime.Serialization.KnownType(typeof(JobB))]
And each of those types must be marked with: 并且每种类型都必须标记为:
[System.Runtime.Serialization.DataContract]
Note: Since JobBase is abstract, no reason to mark it with KnownType... 注意:由于JobBase是抽象的,因此没有理由用KnownType对其进行标记...
That's it. 而已。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.