当前位置: 首页 > 编程语言 > C#

C#学习教程:.NET4.5中的序列化中断Share

时间:2023-04-10 20:12:55 C#

.NET4.5中的序列化中断我们有一个仅发生在.NET4.5中的序列化问题-同样的代码在.NET4中工作正常。我们正在尝试序列化继承的带有多个字段的类型,基类和继承类都标有Seri??alizableAttribute。我们在Web服务的客户端得到一个异常,说服务器中有一个MethodAccessException,服务器本身没有抛出任何异常,这似乎是客户端序列化过程中的问题。请务必注意,我们是在.NET4中编译的,而不是.4.5更新:在实施ISerailize并忽略“Value”属性后,程序运行良好,但这意味着我们不得不放弃序列化该字段。任何帮助是极大的赞赏。谢谢,奥梅尔示例外部细节:System.Web.Services.Protocols.SoapExceptionoccurredHResult=-2146233087Message=System.Web.Services.Protocols.SoapException:服务器无法处理请求。--->System.InvalidOperationException:生成XML文档时出错。--->System.MethodAccessException:尝试通过方法'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write88_DeviceSiteTypeInfo(System.String,System.String,IOSIGHT.Info.DeviceSiteTypeInfo,Boolean,Boolean)'访问方法'IOSIGHT.Info.DeviceSiteTypeInfo.get_Value()'失败。在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write88_DeviceSiteTypeInfo(Stringn,Stringns,DeviceSiteTypeInfoo,BooleanisNullable,BooleanneedType)在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1310_GetSiteTypesResponse(Object[]p)在Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer2089.Serialize(ObjectobjectToSerialize,XmlSerializationWriterwriter)atSystem.Xml.Serialization.XmlSerializer.Serialize(XmlWriterxmlWriter,Objecto,XmlSerializerNamespacesnamespaces,StringencodingStyle,Stringid)---内部异常堆栈跟踪结束---atSystem.Xml.Serialization.XmlSerializer.Serialize(XmlWriterxmlWriter,Objecto,XmlSerializerNamespacesnamespaces,StringencodingStyle,Stringid)在System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(Object[]returnValues,StreamoutputStream)在System.Web.Services.Protocols。System.Web.Services.Protocols.WebServiceHandler.Invoke()处的WebServiceHandler.WriteReturns(Object[]returnValues)---内部异常堆栈跟踪结束---Source=System.Web.ServicesActor=""Lang=""Node=""Role=""StackTrace:在System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage消息,WebResponse响应,StreamresponseStream,BooleanasyncCall)在System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(StringmethodName,Object[]parameters)在IOSIGHT.BLL.localhost.IOSightWS.GetSiteTypes()在C:IOSIGHTCommonIOSight.BLLWebReferenceslocalhostReference.cs:line25019在IOSIGHT.BLL.TypeBankBLL.GetSiteTypes()在C:IOSIGHTCommonIOSight.BLLEntitiesTypeBanksBLL.cs:line477InnerException:附件是基础类和继承类代码:Base:[Serializable()]publicclassTypeBankInfo{#region"Fields"privateint_id=0;私有字符串_Name=string.Empty;私有字符串_description=string.Empty;私有对象_value=null;#endregion#region“构造函数”publicTypeBankInfo(){}publicTypeBankInfo(intID,stringname):this(){this._id=ID;this.Name=名称;}publicTypeBankInfo(intID,stringname,stringdescription):this(ID,name){this._description=description;this._value=值;}publicTypeBankInfo(intID,stringname,stringdescription,objectvalue):this(ID,name,description){this._value=value;}#endregion#region"Properties"publicvirtualstringName{get{returnthis._Name;}设置{this._Name=value;}}publicvirtualstringDescription{get{return_description;}设置{_description=值;}}publicvirtualintID{get{return_id;}设置{_id=int.Parse(value.ToString());}}publicvirtualobject@Value{get{returnthis._value;}设置{this._value=value;}}#endregion}Intheried:[Serializable()]publicclassDeviceSiteTypeInfo:TypeBankInfo,ISerializable{#region"Fields"privateEntityTypeEnum_entitytype=EntityTypeEnum.Site;私有DeviceIOTemplateInfo[]_IOTemplates;私人字幕信息[]_Captions;私有int_parentClassID;#endregion#region“构造函数”publicDeviceSiteTypeInfo(){}publicDeviceSiteTypeInfo(intid,stringname):base(id,name){}publicDeviceSiteTypeInfo(intid,stringname,stringdescription):base(id,name,description){}//特殊构造函数用于d序列化值。publicDeviceSiteTypeInfo(SerializationInfoinfo,StreamingContextcontext){//父字段ID=(int)info.GetValue("_id",typeof(int));Name=(string)info.GetValue("_Name",typeof(string));Description=(string)info.GetValue("_description",typeof(string));//我的字段_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”publicEntityTypeEnumEntityTypeID{get{returnthis._entitytype;}设置{this._entitytype=value;}}[EditorBrowsable(EditorBrowsableState.Never),Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]privatenewobjectValue{get{returnbase.Value;}放{base.Value=值;}}publicCaptionInfo[]Captions{get{returnthis._Captions;}设置{this._Captions=value;}}publicDeviceIOTemplateInfo[]IOTemplates{get{returnthis._IOTemplates;}设置{this._IOTemplates=value;}}publicintParentClassID{get{returnthis._parentClassID;}设置{this._parentClassID=value;}}#endregion#region方法//////在序列化时调用//////序列化信息///contextpublicvoidGetObjectData(SerializationInfoinfo,StreamingContextcontext){//父字段info.AddValue("_id",ID,typeof(int));info.AddValue("_Name",Name,typeof(string));info.AddValue("_description",Description,typeof(string));//我的字段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}在4.5中,XmlSerializer的实现被替换为不依赖于C#编译器的实现。虽然它提供了更好的启动性能和稳定性,但您可能会遇到实现之间的兼容性问题。您能否尝试将以下内容添加到您的app.config文件中,看看是否可以解决问题?如果您担心在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我们希望在即将推出的.NETFramework4.5更新中修复此问题。发布更新后,我将使用下载链接更新帖子。如果您的关键任务应用程序受到影响,请联系Microsoftdotcom的netfx45compat并急需修复。我可以帮助您找到可以协助您的请求的Microsoft支持。我更仔细地查看了您的类型,问题可能是由以下冲突引起的:基础上的publicvirtualobject@Value{}和派生类上的privatenewobjectValue{}。这是我正在尝试做的两件事:尝试公开一个“新对象值”。可能存在成员访问问题。尝试将Base的“@Value”重命名为“BaseValue”或其他合适的名称。使用“@”符号时可能会出现问题。我也有这样的序列化失败。在我的例子中,它是由[DefaultValue(..)]属性的类型不匹配引起的。对于小数类型的属性,我有一个额外的默认值“1.0d”(双“1.0d”)。似乎XmlSerializer的新实现无法再转换这些值,但旧的可以。也可以通过在“App.config”中添加属性来切换回旧版本的XmlSerializer,但Microsoft(和我)不推荐这样做。希望这对某人有帮助。以上就是《C#学习教程:.NET4.5中的序列化中断》的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。涉及侵权,请点击维权联系管理员删除。如需转载请注明出处: