title: Intermittent Catastrophic Failure exception with .Net application via PsExec
body: <p>we're seeing an intermittent problem when we try to run a .Net 4.0 application on a remote computer via PsExec.
  The target computer is Windows Server 2008 SP2 64-bit with .Net 4.0 and .Net 4.5 installed. </p>
  
  <p>I've been told that this has been happening here since .Net 4.5 was installed, but I'm new to the project, so I can't confirm that this is when the problem started happening.</p>
  
  <p>The code that is failing is doing this:</p>
  
  <pre><code>var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  return (ParameterData)config.GetSection("ParameterData");
  </code></pre>
  
  <p>Here's the stack trace:</p>
  
  <pre><code>System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)) ---&gt; System.Runtime.InteropServices.COMException: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
     at System.Security.Policy.PEFileEvidenceFactory.GetLocationEvidence(SafePEFileHandle peFile, SecurityZone&amp; zone, StringHandleOnStack retUrl)
     at System.Security.Policy.PEFileEvidenceFactory.GenerateLocationEvidence()
     at System.Security.Policy.PEFileEvidenceFactory.GenerateEvidence(Type evidenceType)
     at System.Security.Policy.AssemblyEvidenceFactory.GenerateEvidence(Type evidenceType)
     at System.Security.Policy.Evidence.GetHostEvidenceNoLock(Type type)
     at System.Security.Policy.Evidence.GetHostEvidence(Type type, Boolean markDelayEvaluatedEvidenceUsed)
     at System.Security.Policy.AppDomainEvidenceFactory.GenerateEvidence(Type evidenceType)
     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&amp; typeName)
     at System.Configuration.ClientConfigPaths.GetTypeAndHashSuffix(AppDomain appDomain, String exePath)
     at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
     at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
     at System.Configuration.ClientConfigurationHost.GetStreamName(String configPath)
     at System.Configuration.BaseConfigurationRecord.InitConfigFromFile()
     --- End of inner exception stack trace ---
     at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
     at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
     at System.Configuration.Configuration..ctor(String locationSubPath, Type typeConfigHost, Object[] hostInitConfigurationParams)
     at System.Configuration.ClientConfigurationHost.OpenExeConfiguration(ConfigurationFileMap fileMap, Boolean isMachine, ConfigurationUserLevel userLevel, String exePath)
     at System.Configuration.ConfigurationManager.OpenExeConfigurationImpl(ConfigurationFileMap fileMap, Boolean isMachine, ConfigurationUserLevel userLevel, String exePath, Boolean preLoad)
     at System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel userLevel)
  </code></pre>
  
  <p>This is executed during our automated test build deployment process.  I've been able to boil it down to a simple executable that does the code above, and I call PsExec from my Windows 7 development machine, but the problem's intermittent.
  It seems to occur more regularly if I wait several minutes between invocations of PsExec.  I started looking at this problem today, and was able to repro it pretty regularly in the morning, but not very easily in the afternoon.</p>
  
  <p>I also see warnings like this in the Event Log:</p>
  
  <p>Windows detected your registry file is still in use by other applications or services. The file will be unloaded now. The applications or services that hold your registry file may not function properly afterwards.  </p>
  
  <p>DETAIL - 
   1 user registry handles leaked from \Registry\User\mySID:
  Process 5568 () has opened key \REGISTRY\USER\mySID</p>
  
  <p>I don't know how the Event Log errors are related to the exception, if at all.</p>
  
  <p>The PsExec command line is: PsExec /accepteula \\myServerName -d "path_to_exe"</p>
  
  <p>I'm wondering if it's a problem with the user profile service, so I was thinking about not loading the profile when calling PsExec, but since it's so intermittent, I'm not sure how I would know if I've fixed it.
  Searching the internet shows that some people think it might be related to impersonation, but nothing that directly matches my situation.</p>
  
  <p>Has anyone seen anything like this?  This is really weird.</p>
  
  <p>Thanks!
  Phil</p>