ASP.NET Web 服务看不到 appSettings

发布于 2024-12-02 10:42:58 字数 768 浏览 3 评论 0原文

有谁知道 ASP.NET 2.0 Web 服务可能无法从 web.config 读取 appSettings 值的情况吗?我似乎正好遇到这个问题 - 代码认为 appSettings 为空,但实际上它不是。

更详细地说:此代码:

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

错误地显示应用程序设置中没有数据。

我的网络配置看起来像这样,

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

我已经验证代码可以看到 web.config 文件(通过注释掉文件中的所有内容的简单设备,并看到当我尝试运行该服务时,它抱怨 web.config 文件存在问题)。配置无效)。

该代码是生产代码,原则上可以工作,只是在我的机器上显然没有 - 所以我猜测问题与我的环境有关,几乎可以肯定不是代码的问题。

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 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.

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

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

发布评论

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

评论(1

红颜悴 2024-12-09 10:42:58

啊,看来原因简单得离谱。我只需要重新编译 VB 代码!发生的事情是这样的,因为我(正确地,确实如此)确信 VB 代码没有任何问题,所以我将所有精力集中在摆弄 web.config 和 IIS 设置上(因为这些可能会导致错误)。可以想象,我的机器和已知的实时工作程序之间可能有所不同)。最终我放弃了这一点,并决定通过更改 VB 代码以使用硬编码值来暂时解决这个问题。一旦我这样做并重建,我发现程序现在正确地选择了 appSettings (并且我的 hack 变得不必要了)。

所以,我的猜测是,VB 代码不知何故与 web.config 分离,需要重新编译才能重新附加它。我有点困惑,因为我认为 ASP.NET 会自动检测 web.config 的更改,因此无论如何都会重新编译,但显然不会。

如果有人可以通过解释 ASP.NET 中可能发生的情况来满足我的好奇心,这些情况可能导致需要显式代码重新编译才能正确读取 web.config,那么我会将其标记为我最初问题的答案。 (如果一天左右没有人这样做,我会将这篇文章标记为答案)。

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).

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