开发者

ASP.NET web service can't see the appSettings

开发者 https://www.devze.com 2023-04-01 11:08 出处:网络
Is anyone aware of any situations in which an ASP.NET 2.0 webservice might be unable to read the appSettings values from the web.config? I seem to have exactly that problem - the code thinks the appSe

Is anyone aware of any situations in which an ASP.NET 2.0 webservice might be unable to read the appSettings values from the web.config? I seem to have exactly that problem - the code thinks the appSettings is empty when it isn't.

In more detail: This code:

    Dim settings = ConfigurationManager.AppSettings
    Dim count = settings.Count   ' always gives zero

Incorrectly shows that there is no data in the app settings.

My web config looks like this

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <!-- lots of other stuff -->
  <appSettings>
    <add key="SomeKey" value="Some Data" />
    <!-- other keys -->
  </appSettings>
</configuration>

I've verified that the code can see the web.config file (by the simple device of commenting out everything in the file, and seeing that when I try to run the service, it complains that the web.config is invalid).

The code is production 开发者_JS百科code which does in principle work, just not apparently on my machine - so I'm guessing the problem has something to do with my environment, it's almost certainly not a problem with the code.


Ah, it seems the reason was absurdly simple. I just needed to recompile the VB code! What happened was that, because I was (correctly, as it happens) convinced there was nothing wrong with the VB code, I focused all my efforts on fiddling with the web.config and with the IIS settings (because those were the things that might conceivably be different between my machine and the known working live program). Eventually I gave up on that, and decided to temporarily work around by changing the VB code to use hardcoded values instead. As soon as I did that and rebuilt, I found that the program now picked up the appSettings correctly (and my hack became unnecessary).

So, my guess is that somehow the VB code became detached from the web.config, and needed recompiling to re-attach it. I'm somewhat puzzled because I thought that ASP.NET would automatically detect changes to the web.config and so recompile anyway, but evidently not.

If anyone can satisfy my curiosity by explaining what might have been going on in ASP.NET that could result in an explicit code-recompile being necessary to read the web.config correctly, then I'll mark that as the answer to my original question. (If noone does after a day or so, I'll mark this post as the answer).

0

精彩评论

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

关注公众号