Windows 服务,app.config 中 appSettings 的奇怪行为

发布于 2024-11-09 23:31:35 字数 2571 浏览 5 评论 0原文

我正在开发一个具有共享核心组件的项目,该组件使用相应配置文件中的 部分。

这对于使用 web.config 的 asp.net Web 部件效果很好。

但是,有一个 Windows 服务使用相同的共享核心组件,它(由于各种原因)直接从内部访问配置数据(即嵌入调用 ConfigurationManager.AppSettings["key"])不能轻易重构。

这不是问题,但我发现 Web 服务似乎无法获取我添加到其 app.config 中的 appSettings 值。当我将其部署到开发服务器时,它当然会变成 ServiceName.exe.config 并且配置文件运行正常(它还包含一些 类型安全设置,它们的工作方式为 由于我无法轻松更改共享组件,因此

我不得不在服务的 app.config 文件中使用

:结构 。看起来不错:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="xxxxx.UploadManagerService.UploadManager" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <xxxxx.UploadManagerService.UploadManager>
            <setting name="NumberOfUploaderThreads" serializeAs="String">
                <value>2</value>
            </setting>
             :
        </xxxxx.UploadManagerService.UploadManager>
    </applicationSettings>

  <appSettings>
    <add key="keyname" value="value" />
     :
  </appSettings>

</configuration>

(其中 : 表示“更多相同”:-)

服务运行正常,除非核心组件中的方法尝试访问任何

。要让它与 Windows 服务正常工作吗?我看不出它不应该工作的任何原因,但它不工作(当它尝试访问任何值时抛出异常)

。它掉落的地方over:

return SendEmailViaAmazonSES(
          new List<string> { clientEmailAddress },
            ConfigurationManager.AppSettings["SalesEmail"],
            "Order Confirmation.",
            content);

...幸运的是,它被困在 try:catch 中,因此没有任何事情会失败,但是那些 ConfigurationManager.AppSettings["key"] 调用在整个过程中都被使用,我无法在没有重大影响的情况下更改它们对已经使用该核心组件的其他系统的影响。

有什么想法吗?

我检查过的其他事情:服务配置文件与服务 exe 位于同一文件夹中,开发配置确实包含正确的值。

编辑 25/5

因为该服务仅调用一些需要访问 值的方法,所以我只是通过将这些方法复制到服务本身并使用 中的值。这并不理想,我仍然非常想知道为什么这不适用于 Windows 服务,但我等不起,所以我采取了务实的决定,同时“捏造它”。我总是可以在稍后的某个时候回到这个问题,或者(正如发生的那样)忘记所有这一切;-)

I'm working on a project that has a shared core component which makes use of an <appSettings /> section in the corresponding configuration file.

This works fine for the asp.net web part, which uses web.config.

However, there is a Windows service which uses this same shared core component, which (for various reasons) accesses configuration data directly from within (i.e. embedded calls to ConfigurationManager.AppSettings["key"]) which I can't easily refactor.

This wouldn't be a problem but I am finding that the web service doesn't seem to be able to pick up the appSettings values that I have added to its app.config. When I deploy this to a dev server, of course it becomes ServiceName.exe.config and the configuration file is otherwise operating correctly (it also contains some <applicationSettings /> typesafe settings which are working as expected.

Since I can't easily change the shared component, I am stuck with somehow having to work with <appSettings /> in the Service's app.config file.

Things I've checked: structure seems fine:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="xxxxx.UploadManagerService.UploadManager" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <xxxxx.UploadManagerService.UploadManager>
            <setting name="NumberOfUploaderThreads" serializeAs="String">
                <value>2</value>
            </setting>
             :
        </xxxxx.UploadManagerService.UploadManager>
    </applicationSettings>

  <appSettings>
    <add key="keyname" value="value" />
     :
  </appSettings>

</configuration>

(where : means 'more of the same' :-)

Service runs okay except for where a method in the core component attempts to access any of the <appSettings /> values.

Is there any way to get this to work properly with a Windows service? I can't see any reason why it shouldn't just work, but it doesn't (throwing an exception when it tries to access any of the values).

Here's a frag of the kind of place where it falls over:

return SendEmailViaAmazonSES(
          new List<string> { clientEmailAddress },
            ConfigurationManager.AppSettings["SalesEmail"],
            "Order Confirmation.",
            content);

...which is fortunately trapped in a try:catch and therefore nothing is falling over but those ConfigurationManager.AppSettings["key"] calls are used throughout and I can't change them without major impact to other systems which already use this core component.

Any ideas?

Other things I've checked: service config file IS in same folder as service exe, dev config DOES contain the right values.

EDIT 25/5

Because the service only calls a few methods which require access to the <appSettings /> values, I've simply cheated by copying those methods into the service itself and using values in <applicationSettings /> instead. It's not ideal and I'm still very keen to find out why this doesn't work for a Windows Service, but I couldn't afford to wait so I've taken the pragmatic decision to 'fudge it' in the meantime. I can always return to this at some later point, or (as happens) forget all about it ;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潇烟暮雨 2024-11-16 23:31:35

文件权限

查看运行 Windows 服务的用户是否对其 .config 文件具有读取权限。

Sysinternals Process Monitor

使用 Sysinternals Process Monitor 并按您的服务名称进行筛选,以查看是否进程确实尝试通过您期望的路径访问您的文件。

File permissions

See if the user under which your Windows service runs has read access to its .config file.

Sysinternals Process Monitor

Use Sysinternals Process Monitor and filter by your service name to see if the process really tries to access your file via the path you expect it to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文