开发者

Windows Service, strange behaviour with appSettings in app.config

开发者 https://www.devze.com 2023-03-08 04:27 出处:网络
I\'m working on a project that has a shared core component which makes use of an <appSettings /> section in the corresponding configuration file.

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" />
        </s开发者_如何学编程ectionGroup>
    </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 ;-)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消