启动Windows服务时出现TypeInitializationException,因为无法创建config部分

启动Windows服务时,我在特定的Windows Server 2008 R2计算机上遇到了一个奇怪的错误(它可以在其他2008 R2计算机上运行)。 该服务使用Common.Logging和log4net 。 但是,在此特定计算机上,无法创建Common.Logging的配置节处理程序。

它使用以下堆栈跟踪失败(格式化以获得更好的可读性)。 最令我惊讶的是SecurityException 。 是什么导致这种情况?

有人有线索吗?

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

它继续:

 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.b__3() at Common.Logging.Configuration.ArgUtils.c__DisplayClass13.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) 

我的配置看起来像这样(无论如何,它的Common.Logging部分)。

   

当客户从我们的网站下载我们的zip(ASP.NET应用程序)文件时,我发现了一些奇怪的事情。 由于安全function“此文件来自另一台计算机,可能会被阻止以帮助保护此计算机”。

解锁文件解决了所有奇怪的问题,这可以解释为什么这只发生在具有完全相同配置的一些计算机上。


只有在文件属性对话框的“常规”选项卡中实际阻止文件时,才会显示“取消阻止”选项:

在此处输入图像描述

如果项目>属性>应用程序选项卡>启动对象下拉列表配置为指向Windows服务的Program类,您也可以在Visual Studio 2013中签入。 如果未设置该值,则服务将无法以System.TypeInitializationException启动。

您似乎需要授予服务帐户权限才能访问配置文件。
编辑:实际上在第二眼看来它似乎不是问题,因为它实际上是读取配置文件,但你应该仔细检查权限。

更新:这是.NET 4.0的一个已知问题,但有一个解决方法 – 请参阅http://social.msdn.microsoft.com/Forums/en-US/clr/thread/1e14f665-10a3-426b-a75d-4e66354c5522 。

原来我是因为我从.NET认为不安全的网络映射驱动器运行可执行文件