更新后 XmlSerializer 停止工作 [英] XmlSerializer stopped working after updates

查看:42
本文介绍了更新后 XmlSerializer 停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XmlSerializer.到目前为止,我没有遇到任何问题.我将 Silverlight 从 4 更新到 5,同时还将 WCF RIA 服务从 v1 SP1 更新到 v1 SP2.现在下面这行给了我一个错误.

I'm using XmlSerializer. I've had no problems with it until now. I updated Silverlight from 4 to 5 and at the same time also updated the WCF RIA Services from v1 SP1 to v1 SP2. Now the following line gives me an error.

XmlSerializer s = new XmlSerializer(typeof(MyCustomObject));

错误是:

System.InvalidOperationException:System.ServiceModel.DomainServices.Client.EntityConflict 无法序列化,因为它没有无参数构造函数.

我使用的对象(示例中的 MyCustomObject)没有任何改变,所以我开始认为是 SL5 或新的 RIA 服务破坏了我的代码.我没有找到任何重大更改文档或提到这可能发生.我不知道为什么 EntityConflict 有问题,因为我没有在我的对象中使用任何实体.

The object I'm using (MyCustomObject in the sample) has not changed in any way so I'm starting to think it's either SL5 or the new RIA Services that is breaking my code. I didn't find any breaking changes document or mentions that this could happen. I don't know why it has a problem with EntityConflict since I'm not using any entities within my object.

有没有人看到过这样的错误和/或知道如何解决它?

Has anyone seen an error like this and/or know how to solve it?

更新!

错误消息在 EntityConflict 之前说的最后一个属性是一个实体.我认为这有所作为,但它以前一直有效.我还想知道为什么序列化程序已经尝试在构造函数中序列化对象?

The final property that the error message says before EntityConflict is an Entity. I think that makes a difference but it has been working before. I'd also like to know why the serializer already tries to serialize the object in the constructor?

推荐答案

public static XmlSerializer GetEntityXmlSerializer<TEntity>()
         where TEntity : Entity
{ 
    XmlAttributes ignoreAttribute = new XmlAttributes()
                                    {
                                         XmlIgnore = true,
                                    };

    // use base class of Entity, 
    // if you use type of implementation 
    // you will get the error.
    Type entityType = typeof(Entity);

    var xmlAttributeOverrides = new XmlAttributeOverrides();
    xmlAttributeOverrides.Add(entityType, "EntityConflict", ignoreAttribute);
    xmlAttributeOverrides.Add(entityType, "EntityState", ignoreAttribute);

    return new XmlSerializer(typeof(TEntity), xmlAttributeOverrides);
}

这篇关于更新后 XmlSerializer 停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆