简体   繁体   English

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

[英]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.

相关问题 序列化视图模型时出错:“类型&#39;System.Web.HttpPostedFileWrapper&#39;无法序列化。” - Error serializing view model: “Type 'System.Web.HttpPostedFileWrapper' cannot be serialized.” 使用WCF DataContract的InvalidDataContractException(“类型无法序列化”)错误 - InvalidDataContractException (“Type cannot be serialized”) error using WCF DataContract 我在这个实现中做错了什么? - What I'm doing wrong with this implementation? 我无法弄清楚这个基本的NHibernate查询在做什么错 - I cannot figure out what I'm doing wrong with this basic NHibernate query AnonymousType无法序列化。 考虑使用DataContractAttribute属性对其进行标记 - AnonymousType cannot be serialized. Consider marking it with the DataContractAttribute attribute 数据合同异常。 无法序列化 - Datacontract exception. Cannot be serialized 我收到一个错误:使用未分配的局部变量“艺术家”,不知道我在做什么错? - I get an error: use of unassigned local variable 'artist', don't know what i'm doing wrong? 无法将当前的JSON数组(例如[1,2,3])反序列化为类型……我在做什么错? - Cannot deserialize the current JSON array (e.g. [1,2,3]) into type… what am I doing wrong? LINQ Select-CS0411无法从用法中推断出类型实参-我在做什么错? - LINQ Select - CS0411 The type arguments cannot be inferred from the usage - What am I doing wrong? 看到“使用通用方法时无法将表达式转换为类型”时,我在做什么错? - What am I doing wrong when seeing “cannot convert expression to type while using a generic method”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM