c# - 启动 Windows 服务时出现 TypeInitializationException,因为无法创建配置部分

标签 c# .net security configuration securityexception

启动 Windows 服务时,我在特定的 Windows Server 2008 R2 机器(它在其他 2008 R2 机器上工作)上出现奇怪的错误。该服务使用 Common.Logginglog4net .但是,在这台特定的机器上,无法创建 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.<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)

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

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

最佳答案

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

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


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

enter image description here

关于c# - 启动 Windows 服务时出现 TypeInitializationException,因为无法创建配置部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3452077/

相关文章:

security - JSF 2.0 如何防止 CSRF

c# - .net 中的 IP 路由表查找

c# - MVC 中使用响应重定向

c# - 在复杂/单窗口 WPF 应用程序中使用 Thread 的最佳方式

.net - 命名空间 system.data.entity 中不存在命名空间名称 Infrastructure

javascript - 为什么浏览器允许设置一些没有 CORS 的 header ,但不允许设置其他 header ?试图避免预检

ASP.NET SSL 身份验证票证安全吗?

c# - Json.net JsonIgnore 不适用于嵌套类

c# - 必须在哪里进行依赖注入(inject)注册?

c# - 列表与字典(哈希表)