15

启动 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>
4

4 回答 4

18

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

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


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

在此处输入图像描述

于 2010-08-12T09:59:41.380 回答
3

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

于 2014-04-04T17:03:25.327 回答
1

似乎您需要授予服务帐户访问配置文件的权限。
编辑:实际上乍一看,这似乎不是问题所在,因为它实际上是在读取配置文件,但是无论如何您都应该仔细检查权限。

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

于 2010-08-10T18:37:55.717 回答
0

结果对我来说是因为我正在从.NET认为不安全的网络映射驱动器运行可执行文件

于 2014-11-15T02:59:58.537 回答