.NET 4.5中的序列化出现问题

15
我们在.NET 4.5中出现了序列化问题,而相同的代码在.NET 4中运行良好。我们正在尝试对继承类型进行序列化,基类和派生类都带有 SerializableAttribute 标记。在 Web 服务的客户端方面,我们收到了一个 异常,指示服务器上发生了 MethodAccessException ,而服务器本身并未抛出任何异常,似乎是客户端序列化过程中出现了问题。 值得注意的是,我们正在使用 .NET 4 进行编译,而非 .NET 4.5。
更新:实现了 ISerailize 并忽略了“Value”属性后,程序确实成功运行了,但这意味着我们不得不放弃序列化此字段。
非常感谢您提供任何帮助。 谢谢, Omer 异常详情:
System.Web.Services.Protocols.SoapException occurred
  HResult=-2146233087
  Message=System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.MethodAccessException: Attempt by method 'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write88_DeviceSiteTypeInfo(System.String, System.String, IOSIGHT.Info.DeviceSiteTypeInfo, Boolean, Boolean)' to access method 'IOSIGHT.Info.DeviceSiteTypeInfo.get_Value()' failed.
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write88_DeviceSiteTypeInfo(String n, String ns, DeviceSiteTypeInfo o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1310_GetSiteTypesResponse(Object[] p)
   at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer2089.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
   at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
   at System.Web.Services.Protocols.WebServiceHandler.Invoke()
   --- End of inner exception stack trace ---
  Source=System.Web.Services
  Actor=""
  Lang=""
  Node=""
  Role=""
  StackTrace:
       at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
       at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
       at IOSIGHT.BLL.localhost.IOSightWS.GetSiteTypes() in C:\IOSIGHT\Common\IOSight.BLL\Web References\localhost\Reference.cs:line 25019
       at IOSIGHT.BLL.TypeBankBLL.GetSiteTypes() in C:\IOSIGHT\Common\IOSight.BLL\Entities\TypeBanksBLL.cs:line 477
  InnerException: 

以下是基类和派生类的代码: 基类:

[Serializable()]
public class TypeBankInfo
{


    #region "Fields"
    private int _id = 0;
    private string _Name = string.Empty;
    private string _description = string.Empty;
    private object _value = null;

    #endregion

    #region "Constructors"
    public TypeBankInfo()
    {
    }

    public TypeBankInfo(int ID, string name)
        : this()
    {
        this._id = ID;
        this.Name = name;
    }

    public TypeBankInfo(int ID, string name, string description)
        : this(ID, name)
    {
        this._description = description;
        this._value = Value;
    }

    public TypeBankInfo(int ID, string name, string description, object value)
        : this(ID, name, description)
    {
        this._value = value;
    }

    #endregion

    #region "Properties"
    public virtual string Name
    {
        get
        {
            return this._Name;
        }
        set
        {
            this._Name = value;
        }
    }

    public virtual string Description
    {
        get
        {
            return _description;
        }
        set
        {
            _description = value;
        }
    }

    public virtual int ID
    {
        get
        {
            return _id;
        }
        set
        {
            _id = int.Parse(value.ToString());
        }
    }


    public virtual object @Value
    {
        get
        {
            return this._value;
        }
        set
        {
            this._value = value;
        }
    }

    #endregion

}

Inherited:

[Serializable()]
public class DeviceSiteTypeInfo : TypeBankInfo, ISerializable
{


    #region "Fields"
    private EntityTypeEnum _entitytype = EntityTypeEnum.Site;
    private DeviceIOTemplateInfo[] _IOTemplates;
    private CaptionInfo[] _Captions;
    private int _parentClassID;
    #endregion

    #region "Constructors"
    public DeviceSiteTypeInfo()
    {
    }

    public DeviceSiteTypeInfo(int id, string name)
        : base(id, name)
    {
    }

    public DeviceSiteTypeInfo(int id, string name, string description)
        : base(id, name, description)
    {
    }

    // The special constructor is used to deserialize values. 
    public DeviceSiteTypeInfo(SerializationInfo info, StreamingContext context)
    {
        //parent  fields
        ID = (int)info.GetValue("_id", typeof(int));
        Name = (string)info.GetValue("_Name", typeof(string));
        Description = (string)info.GetValue("_description", typeof(string));


        //my fields
        _entitytype = (EntityTypeEnum)info.GetValue("_entitytype", typeof(EntityTypeEnum));
        _IOTemplates = (DeviceIOTemplateInfo[])info.GetValue("_IOTemplates", typeof(DeviceIOTemplateInfo[]));
        _Captions = (CaptionInfo[])info.GetValue("_Captions", typeof(CaptionInfo[]));
        _parentClassID = (int)info.GetValue("_parentClassID", typeof(int));

    }



    #endregion

    #region "Properties"
    public EntityTypeEnum EntityTypeID
    {
        get
        {
            return this._entitytype;
        }
        set
        {
            this._entitytype = value;
        }
    }



    [EditorBrowsable(EditorBrowsableState.Never), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    private new object Value
    {
        get
        {
            return base.Value;
        }
        set
        {
            base.Value = value;
        }
    }

    public CaptionInfo[] Captions
    {
        get
        {
            return this._Captions;
        }
        set
        {
            this._Captions = value;
        }
    }

    public DeviceIOTemplateInfo[] IOTemplates
    {
        get
        {
            return this._IOTemplates;
        }
        set
        {
            this._IOTemplates = value;
        }
    }

    public int ParentClassID
    {
        get
        {
            return this._parentClassID;
        }
        set
        {
            this._parentClassID = value;
        }
    }

    #endregion


    #region Methods

   /// <summary>
   /// Called on serialization
   /// </summary>
   /// <param name="info">serialiation info</param>
   /// <param name="context">context</param>
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        // parent fields
        info.AddValue("_id", ID, typeof(int));
        info.AddValue("_Name", Name, typeof(string));
        info.AddValue("_description", Description, typeof(string));

        //my fields
        info.AddValue("_entitytype", _entitytype, typeof(EntityTypeEnum));
        info.AddValue("_IOTemplates", _IOTemplates, typeof(DeviceIOTemplateInfo[]));
        info.AddValue("_Captions", _Captions, typeof(CaptionInfo[]));
        info.AddValue("_parentClassID", _parentClassID, typeof(int));
    }

    #endregion

}

1
你能发布带有调用堆栈的实际异常吗? - Pete
5个回答

17

在4.5版本中,XmlSerializer的实现被替换为不依赖于C#编译器的实现。虽然这提供了更好的启动性能和稳定性,但您可能会遇到实现之间的兼容性问题。您可以尝试将以下内容添加到您的app.config文件中,看看是否可以解决该问题:


在4.5版本中,XmlSerializer的实现被替换为不再依赖C#编译器的实现。新的实现提供了更好的启动性能和稳定性,但是您可能会遇到旧版和新版之间的兼容性问题。如果出现此类问题,您可以尝试将以下代码添加到您的app.config文件中,以解决问题:
<configuration>
  <system.xml.serialization>
    <xmlSerializer useLegacySerializerGeneration="true"/>
  </system.xml.serialization>
</configuration>

如果你担心这个工作在4.0上无法正常运行,你可以尝试在运行时检测框架的版本,并在运行时动态更改配置,如果运行时是4.5或更高版本。我之前写了一篇博客文章,解释了如何做到这一点:

http://blogs.msdn.com/b/youssefm/archive/2010/01/21/how-to-change-net-configuration-files-at-runtime-including-for-wcf.aspx


谢谢您的建议。我正在使用.NET 4编译我的代码,而不是.NET 4.5 - 因此,这个属性对我不适用。问题出在“Value”属性上,我怀疑它的属性没有被正确序列化。 - omer schleifer
1
@omerschleifer 你确定它不适用吗?即使你编译为 .Net 4.0,如果已安装 .Net 4.5,你的代码实际上将在 .Net 4.5 下运行。 - svick
谢谢,这确实有帮助。它还可以防止在服务器端的调试会话中由相同的序列化问题引起的Visual Studio崩溃。然而,由于我们正在编译.NET 4,并且此代码在源代码控制下,任何未安装.NET 4.5的计算机都无法加载配置文件。对于那些没有安装.NET 4.5框架的客户也是如此。 - omer schleifer
我已经更新了答案。您可以使用此答案检测是否安装了4.5:https://dev59.com/BHVC5IYBdhLWcg3wvT1a。您还可以检查mscorlib的次要版本。 - Youssef Moussaoui

5
我们计划在即将推出的.NET Framework 4.5更新中解决这个问题。一旦更新发布,我会更新帖子并提供下载链接。如果您使用的应用程序受到影响并且需要紧急修复,请通过netfx45compat at Microsoft dot com联系我们。我可以帮助您联系到可以协助您请求的Microsoft支持。

仍然无法正常工作,即使在 .NET 4.6 版本也是如此。https://support.microsoft.com/en-us/kb/2748720 对使用 XmlSerializer 时出现的即将生成的程序集 FileNotFoundException 没有帮助。 - mbx

3

我也遇到过这样的序列化错误。在我的情况中,它是由于 [DefaultValue(..)] 属性的类型不匹配造成的。我的一个属性的默认值是"1.0d"(双精度浮点数),但该属性的类型是 decimal。似乎新实现的 XmlSerializer 无法转换此类值,但旧版可以。还有一种方法是通过在 'App.config' 中添加属性来切换回旧版的 XmlSerializer,但这并不被Microsoft(也不是我)推荐。希望这能帮助某些人。


1
我仔细查看了你的类型,问题很可能是由以下冲突引起的:
public virtual object @Value
{
}

在基础上:
private new object Value
{
}

关于派生类的问题,这里有两个建议:

  1. 尝试将“新对象Value”设为public。可能存在成员访问问题。
  2. 尝试将Base类中的“@Value”重命名为“BaseValue”或其他合适的名称。你使用“@”符号可能存在问题。

0

我最近遇到了同样的问题,并找到了实际原因和正确的解决方案。

这个问题是由XML序列化程序测试属性/字段的默认值引起的。如果字段值与DefaultValue属性匹配,它将不会序列化以节省空间。类型为“decimal”的DefaultValue没有重载,因此十进制值将转换为其中一个重载的浮点数。

您必须明确告诉属性它是十进制数:

[DevaultValue(typeof(decimal), "1.56")]

请注意,在字段上使用 DefaultValue 不会在反序列化时将该字段设置为该值。您仍然需要在构造函数或字段初始化器中设置正确的值:

[DefaultValue(typeof(decimal), "1.56")]
decimal MyValue { get; set; } = 1.56;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接