【问题标题】:XmlSerializer stopped working after updatesXmlSerializer 更新后停止工作
【发布时间】:2012-02-08 13:23:02
【问题描述】:

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

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

错误是:

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

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

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

更新!

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

【问题讨论】:

  • 当我从 SL4 -> SL5 更新我的应用程序时出现同样的问题

标签: silverlight wcf-ria-services xmlserializer


【解决方案1】:
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);
}

【讨论】:

  • 这工作(在 SL5 中)并且我能够序列化我的实体。但是,它不遵循导航属性并序列化这些实体。
【解决方案2】:

我不确定为什么会发生这种情况,RIA 服务实体不是 XmlSerializable 对象,并且实体本身没有使用 [Serializable] 属性进行修饰。您是否在客户端添加了用 [Serializable] 装饰实体的部分类或以某种方式修改了代码生成?

【讨论】:

  • 我以为你解决了这个问题。我喜欢类中的 Serializable 属性,但不幸的是删除它并没有解决它。仍然出现同样的错误。
【解决方案3】:

我通过使用中间可序列化 POCO 对象解决了这个问题,这些对象是我的自定义对象(从实体继承)的副本。 POCO 对象没有从实体继承。我刚刚从原始 Entity 对象更新了它们的值。然后他们很好地连载了。当然,当你反序列化时,你需要从 POCO 对象中更新你的 Entity 对象。

【讨论】:

    猜你喜欢
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多