4

I'm getting this stack trace when I call:

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

The XslCompiledTransform object Loads an XmlReader that uses GetManifestResourceStream to an embedded .xslt file, but I have confirmed that it is getting that information correctly.

I've looked at it quite a bit and narrowed it down to this call, but I am not sure where to go from here. Has anyone else experienced this?

This was on a Windows 8 machine, but I have experienced it on server2008r2 OS

ReddShepherd
  • 467
  • 1
  • 11
  • 24

1 Answers1

17

I am experiencing the same error with .NET 4.5. I only see the error when using nunit 2.6+. It seems to happen when you initialize an XmlSerializer in a sub-AppDomain, with objects stored in the CallContext. The type of the object in the CallContext can't be resolved if the ApplicationBase (the bin-path) is set to something different in the sub-AppDomain. You can see the assembly binding error in the Fusion Log Viewer: http://msdn.microsoft.com/en-us/library/e74a18c4.aspx

In my case, if I copy the assembly with the type in it to nunits bin-path, the error goes away. This is of course not a viable solution.

Have you found the root cause for the error?

EDIT: I fixed it by letting the type inherit MarshalByRefObject: Type cant be resolve in UnitTest after migrating Project from vs2005 to vs2010 (MSTest)

EDIT 2: Alternative fix is to call System.Configuration.ConfigurationManager.GetSection("dummy") prior to the code that fails.

Community
  • 1
  • 1
  • In my case just now, it was (as per @MikeGoatly's comment of yesterday) the Principal associated with the `CallContext`. My work around was to safe the `Thread.CurrentPrincipal` into a local variable and then assign it a `new ClaimsPrincipal()` whilst the Xsl transform was compiled. (And then revert it back afterwards) – Damien_The_Unbeliever May 02 '13 at 14:04
  • 1
    @Andreas Kromann, can you please explain why calling `ConfigurationManager.GetSection()` solves the problem? I've tried it, it works, but I cannot explain it. – RePierre Aug 12 '13 at 06:18
  • 1
    I was running with MSTest (v4.0) and I was getting a runtime error of {"Configuration system failed to initialize"} and Type is not resolved for member XXXX. I added System.Configuration.ConfigurationManager.GetSection("dummy") to the [TestInitialize] and now it is working. I would like to know why also. – spinner_den_g Mar 23 '15 at 20:32
  • 1
    I _think_ this is the explanation for why Fix (2) works: it prompts app config resolution (find app config file, merge with machine config, etc.) before other app code (and libraries like log4net) have the chance to place anything in CallContext. Otherwise, if app config resolution is done later, System.Configuration.ClientConfigPaths can call System.AppDomain.Evidence, which can cross app domains ... and when CallContext is copied, if something doesn't inherit from MarshalByRefObject, the serialization/type resolution error will occur. – Dave Clausen Jan 22 '16 at 21:57