.NET 4.5中的序列化异常

时间:2013-03-28 22:27:08

标签: serialization xml-serialization .net-4.5

我打电话时得到这个堆栈跟踪:

XslCompiledTransform.Transform(XmlDocument.DocumentElement.CreateNavigator(), null, StringWriter)


System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Runtime.Serialization.SerializationException: Type is not resolved for member --MyProject stuff
   at System.AppDomain.GetHostEvidence(Type type)
   at System.Security.Policy.AppDomainEvidenceFactory.GenerateEvidence(Type evidenceType)
   at System.Security.Policy.Evidence.GenerateHostEvidence(Type type, Boolean hostCanGenerate)
   at System.Security.Policy.Evidence.GetHostEvidenceNoLock(Type type)
   at System.Security.Policy.Evidence.RawEvidenceEnumerator.MoveNext()
   at System.Security.Policy.Evidence.EvidenceEnumerator.MoveNext()
   at System.Configuration.ClientConfigPaths.GetEvidenceInfo(AppDomain appDomain, String exePath, String& typeName)
   at System.Configuration.ClientConfigPaths.GetTypeAndHashSuffix(AppDomain appDomain, String exePath)
   at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
   at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
   at System.Configuration.ClientConfigurationHost.get_HasRoamingConfig()
   at System.Configuration.ClientConfigurationHost.IsConfigRecordRequired(String configPath)
   at System.Configuration.BaseConfigurationRecord.hlNeedsChildFor(String configName)
   at System.Configuration.Internal.InternalConfigRoot.GetConfigRecord(String configPath)
   at System.Configuration.ClientConfigurationSystem.OnConfigRemoved(Object sender, InternalConfigEventArgs e)
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationManager.PrepareConfigSystem()
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Xml.XmlConfiguration.XmlReaderSection.get_ProhibitDefaultUrlResolver()
   at System.Xml.XmlTextReaderImpl.get_IsResolverNull()
   at System.Xml.Xsl.QueryReaderSettings..ctor(XmlReader reader)
   at System.Xml.Xsl.Xslt.XsltLoader.Load(Compiler compiler, Object stylesheet, XmlResolver xmlResolver)
   at System.Xml.Xsl.Xslt.Compiler.Compile(Object stylesheet, XmlResolver xmlResolver, QilExpression& qil)
   at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
   at System.Xml.Xsl.XslCompiledTransform.Load(XmlReader stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
   at MyProject

XslCompiledTransform对象将使用GetManifestResourceStream的XmlReader加载到嵌入的.xslt文件中,但我已确认它正确获取该信息。

我已经看了很多,并把它缩小到这个电话,但我不知道从哪里开始。还有其他人经历过这个吗?

这是在Windows 8机器上,但我在server2008r2 OS上体验过它

1 个答案:

答案 0 :(得分:17)

我遇到了与.NET 4.5相同的错误。我只在使用nunit 2.6+时看到错误。当您在子AppDomain中初始化XmlSerializer时,似乎会发生这种情况,其中对象存储在CallContext中。如果将ApplicationBase(bin-path)设置为sub-AppDomain中的不同内容,则无法解析CallContext中对象的类型。您可以在Fusion Log Viewer中看到程序集绑定错误:http://msdn.microsoft.com/en-us/library/e74a18c4.aspx

在我的情况下,如果我将其中包含类型的程序集复制到nunits bin-path,则错误消失。这当然不是一个可行的解决方案。

您是否找到了错误的根本原因?

编辑:我通过让类型继承MarshalByRefObject来修复它:Type cant be resolve in UnitTest after migrating Project from vs2005 to vs2010 (MSTest)

编辑2:替代修复方法是在失败的代码之前调用System.Configuration.ConfigurationManager.GetSection(" dummy")。