Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeInitializationException when starting Windows Service because config section can not be created

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 Common.Logging and log4net. However, on this specific machine the config section handler for Common.Logging can not be created.

It fails with the following stack traces (formatted for better readability). What surprises me most is the SecurityException. What can cause this?

Does anyone have a clue?

System.TypeInitializationException: The type initializer for
    'MyWindowsService.Program' threw an exception.
--->
Common.Logging.ConfigurationException: Failed obtaining configuration for
    Common.Logging from configuration section 'common/logging'.
--->
System.Configuration.ConfigurationErrorsException: An error occurred creating
    the configuration section handler for common/logging: Request failed.
    (C:\Path\MyWindowsService.exe.Config line 7)
--->
System.Security.SecurityException: Request failed.
at System.RuntimeTypeHandle.CreateInstance(
    RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached,
    RuntimeMethodHandleInternal& ctor, Boolean& 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& isRootDeclaredHere)
--- End of inner exception stack trace ---

It continues with:

at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(
    String configKey, Boolean& isRootDeclaredHere)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(
    String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject,
    Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at Common.Logging.LogManager.<>c__DisplayClass6.<BuildLoggerFactoryAdapter>b__3()
at Common.Logging.Configuration.ArgUtils.<>c__DisplayClass13.<Guard>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)

My configuration looks like this (the Common.Logging part of it anyway).

<configSections>
  <sectionGroup name="common">
    <section name="logging"
             type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
  </sectionGroup>
</configSections>

<common>
  <logging>
    <factoryAdapter
     type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter,Common.Logging.Log4net">
      <arg key="configType" value="INLINE" />
    </factoryAdapter>
  </logging>
</common>
like image 692
Ronald Wildenberg Avatar asked Aug 10 '10 18:08

Ronald Wildenberg


People also ask

What is TypeInitializationException?

Most commonly, a TypeInitializationException exception is thrown when a static constructor is unable to instantiate a type. The InnerException property indicates why the static constructor was unable to instantiate the type.


2 Answers

I've seen strange things happening when customers downloaded our zip (ASP.NET application) file from our website. Because of the security feature "This file came from another computer and might be blocked to help protect this computer".

Unblocking the files solved all strange problems, this would explain why this is only happening on some computers with exactly the same configuration.


The Unblock option is shown only when the file is actually blocked in the General tab of the file properties dialog:

enter image description here

like image 184
Flores Avatar answered Oct 27 '22 00:10

Flores


You might also check in Visual Studio 2013 if the Project > Properties > Application Tab > Startup Object dropdown is configured to point to the Program class of the windows service. If the value isn't set, the service will fail to start with System.TypeInitializationException.

like image 24
Stamen Avatar answered Oct 27 '22 00:10

Stamen