title: TypeInitializationException when starting Windows Service because config section can not be created
body: <p>I have a strange error on a specific Windows Server 2008 R2 machine (it works on other 2008 R2 machines) when starting a Windows Service. The service uses <a href="http://netcommon.sourceforge.net/" rel="noreferrer">Common.Logging</a> and <a href="http://logging.apache.org/log4net/index.html" rel="noreferrer">log4net</a>. However, on this specific machine the config section handler for Common.Logging can not be created.</p>
  
  <p>It fails with the following stack traces (formatted for better readability). What surprises me most is the <code>SecurityException</code>. What can cause this?</p>
  
  <p>Does anyone have a clue?</p>
  
  <pre><code>System.TypeInitializationException: The type initializer for
      'MyWindowsService.Program' threw an exception.
  ---&gt;
  Common.Logging.ConfigurationException: Failed obtaining configuration for
      Common.Logging from configuration section 'common/logging'.
  ---&gt;
  System.Configuration.ConfigurationErrorsException: An error occurred creating
      the configuration section handler for common/logging: Request failed.
      (C:\Path\MyWindowsService.exe.Config line 7)
  ---&gt;
  System.Security.SecurityException: Request failed.
  at System.RuntimeTypeHandle.CreateInstance(
      RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean&amp; canBeCached,
      RuntimeMethodHandleInternal&amp; ctor, Boolean&amp; bNeedSecurityCheck)
  at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis,
      Boolean fillCache)
  at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly,
      Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
  at System.Activator.CreateInstance(Type type, Boolean nonPublic)
  at System.Configuration.TypeUtil.CreateInstanceWithReflectionPermission(Type type)
  at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.Init(
      RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord)
  at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.InitWithRestrictedPermissions(
      RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord)
  at System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory(
      FactoryRecord factoryRecord)
  at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(
      String configKey, Boolean&amp; isRootDeclaredHere)
  --- End of inner exception stack trace ---
  </code></pre>
  
  <p>It continues with:</p>
  
  <pre><code>at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(
      String configKey, Boolean&amp; isRootDeclaredHere)
  at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(
      String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject,
      Boolean requestIsHere, Object&amp; result, Object&amp; resultRuntimeObject)
  at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
  at System.Configuration.ConfigurationManager.GetSection(String sectionName)
  at Common.Logging.LogManager.&lt;&gt;c__DisplayClass6.&lt;BuildLoggerFactoryAdapter&gt;b__3()
  at Common.Logging.Configuration.ArgUtils.&lt;&gt;c__DisplayClass13.&lt;Guard&gt;b__12()
  at Common.Logging.Configuration.ArgUtils.Guard[T](Function`1 function,
      String messageFormat, Object[] args)
  --- End of inner exception stack trace ---
  
  at Common.Logging.Configuration.ArgUtils.Guard[T](Function`1 function,
      String messageFormat, Object[] args)
  at Common.Logging.LogManager.BuildLoggerFactoryAdapter()
  at Common.Logging.LogManager.get_Adapter()
  at Common.Logging.LogManager.GetLogger(Type type)
  at MyWindowsService.Program..cctor()
  --- End of inner exception stack trace ---
  
  at MyWindowsService.Program.Main(String[] args)
  </code></pre>
  
  <p>My configuration looks like this (the Common.Logging part of it anyway).</p>
  
  <pre><code>&lt;configSections&gt;
    &lt;sectionGroup name="common"&gt;
      &lt;section name="logging"
               type="Common.Logging.ConfigurationSectionHandler, Common.Logging" /&gt;
    &lt;/sectionGroup&gt;
  &lt;/configSections&gt;
  
  &lt;common&gt;
    &lt;logging&gt;
      &lt;factoryAdapter
       type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter,Common.Logging.Log4net"&gt;
        &lt;arg key="configType" value="INLINE" /&gt;
      &lt;/factoryAdapter&gt;
    &lt;/logging&gt;
  &lt;/common&gt;
  </code></pre>