【问题标题】:Windows Service cannot access Castle Windsor config data in app.configWindows 服务无法访问 app.config 中的 Castle Windsor 配置数据
【发布时间】:2011-03-29 00:35:34
【问题描述】:

通过将配置数据放在 web.config 中,我在几个 Web 应用程序中使用 Castle Windsor。

所以当我决定在 Windows 服务中使用它时,我认为这很容易。我的 app.config 文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,Castle.Windsor" />
  </configSections>
  ...various other stuff...
  <castle>
    <components>
      ...component listings...
    </components>
  </castle>
</configuration>

我正在像这样创建我的容器:

WindsorContainer windsorContainer = new WindsorContainer(new XmlInterpreter());

但疯狂的是,当应用程序运行时,我得到了

Could not find section 'castle' in the configuration file associated with this domain.
   at Castle.Core.Resource.ConfigResource..ctor(String sectionName)
   at Castle.Core.Resource.ConfigResource..ctor()
   at Castle.Windsor.Configuration.Interpreters.AbstractInterpreter..ctor()
   at Castle.Windsor.Configuration.Interpreters.XmlInterpreter..ctor()
   ...application stack trace...

我有点迷糊了。有人有什么建议吗?

干杯

大卫

【问题讨论】:

    标签: .net configuration castle-windsor app-config


    【解决方案1】:

    我似乎想起了一些关于 app.config 文件和 Windows 服务的事情。由于实际“运行”您的服务的应用程序是 svchost.exe,并且该 exe 存在于 %windir%\system32 中,因此您需要配置文件。试着把它放在那里 (%windir%\system32) 看看会发生什么。

    【讨论】:

    • 该应用程序从 app.config 中读取其他配置数据很好,但 Castle 可能正在您提到的位置查找。我去看看看看。
    • 我现在又遇到了一个错误,哈哈!我认为这意味着您的建议有效。您已经回答了我的问题,但作为一种解决方法我无法接受,因此我将研究替代配置选项。
    • 我通过将 windsor 配置文件的位置传递给 XmlInterpreter 的构造函数解决了这个问题(参见相关代码)。
    【解决方案2】:

    好吧,让我们来看看显而易见的。可执行文件将使用myprog.exe.config 进行配置。 app.config 应复制到该文件名并与 .exe 一起放置在输出文件夹中。你有那个包含服务可执行文件的文件吗?

    【讨论】:

    • 为什么 cmets 有最小字符限制?!这个网站是精神的。
    • 文件是否有城堡部分(输出目录中您从中运行服务的部分)。您可能会遇到一些奇怪的合并问题...
    • @Krzysztof:你的意思是我在 app.config 文件中包含了城堡部分吗?如果是这样的话。还是你的意思是别的?
    • 我的意思是输出目录中的结果文件。你能确定它确实有这个部分吗?
    【解决方案3】:

    为了解决 windows 服务无法访问配置数据或部分找不到对我有用的问题,以这种方式创建了容器: new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 2011-08-23
      相关资源
      最近更新 更多