启动Windows服务时由于无法创建配置部分而导致TypeInitializationException [英] TypeInitializationException when starting Windows Service because config section can not be created

查看:697
本文介绍了启动Windows服务时由于无法创建配置部分而导致TypeInitializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在启动Windows服务时,我在特定的Windows Server 2008 R2计算机(它在其他2008 R2计算机上工作)中出现了一个奇怪的错误。该服务使用 Common.Logging log4net 。但是,在这个特定的机器上,无法创建Common.Logging的配置节处理程序。



它失败,并出现以下堆栈跟踪(格式化以提高可读性)。让我最惊讶的是 SecurityException

 是什么原因导致的? System.TypeInitializationException:
'MyWindowsService.Program'的类型初始化程序抛出异常。
--->
Common.Logging.ConfigurationException:无法从配置节common / logging中获取
Common.Logging的配置。
--->
System.Configuration.ConfigurationErrorsException:创建
时出错通用/日志记录的配置节处理程序:请求失败。
(C:\Path \MyWindowsService.exe.Config第7行)
--->
System.Security.SecurityException:请求失败。
at System.RuntimeTypeHandle.CreateInstance(
RuntimeType type,Boolean publicOnly,Boolean noCheck,Boolean& canBeCached,
RuntimeMethodHandleInternal& ctor,Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow (System.Activator)上的System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly,
Boolean skipVisibilityChecks,Boolean skipCheckThis,Boolean fillCache)
的值(布尔值publicOnly,Boolean skipCheckThis,
Boolean fillCache) CreateInstance(Type type,Boolean nonPublic)
在System.Configuration.TypeUtil.CreateInstanceWithReflectionPermission(Type type)
在System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.Init(
RuntimeConfigurationRecord configRecord,FactoryRecord factoryRecord)
System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.InitWithRestrictedPermissions(
RuntimeConfigurationRecord configRecord,FactoryRecord factoryRecord)
在System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory(
FactoryRecord factoryRecord)
在系统.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(
String configKey,Boolean& isRootDeclaredHere)
---内部异常堆栈跟踪的结束---

 在System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(
String configKey,Boolean& isRootDeclaredHere)
at System.Configuration.BaseConfigurationRecord上的System.Configuration.BaseConfigurationRecord.GetSectionRecursive(
String configKey,Boolean getLkg,Boolean checkPermission,Boolean getRuntimeObject,
Boolean requestIsHere,Object& result,Object& resultRuntimeObject)
。 <> c__DisplayClass6。< BuildLoggerFactoryAdapter> b__3()
at(System.Configuration.ConfigurationManager.GetSection(String sectionName)
Common.Logging.Configuration.ArgUtils。<> c__DisplayClass13。< Guard> b__12()
at Common.Logging.Configuration.ArgUtils.Guard [T](函数`1函数,
String messageFormat ,Object [] args)
---内部异常堆栈跟踪的结束 -

在Common.Logging.Configuration.ArgUtils.Guard [T](函数`1函数,
String messageFormat,Object [] args)
在Common.Logging.LogManager.BuildLoggerFactoryAdapter()
在Common.Logging.LogManager.get_Adapter()
在Common.Logging.LogManager。 GetLogger(Type type)
在MyWindowsService.Program..cctor()
---内部异常堆栈跟踪结束 -

在MyWindowsService.Program.Main [] args)



我的配置看起来像这样(它的Common.Logging部分) / p>

 < 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 =configTypevalue =INLINE/>
< / factoryAdapter>
< / logging>
< / common>


解决方案

zip(ASP.NET应用程序)文件从我们的网站。由于安全功能此文件来自另一台计算机,可能被阻止以帮助保护此计算机。



解除阻止文件解决了所有奇怪的问题,这将解释为什么






解除封锁选项是仅在文件属性对话框的常规选项卡中实际阻止该文件时显示:




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>

解决方案

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 om 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:

这篇关于启动Windows服务时由于无法创建配置部分而导致TypeInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆