Dave Hanna
Dave Hanna

Reputation: 2521

AppSettings values not being picked up in web.config in subdirectory

We have a set of WCF Services being hosted in IIS. In our architecture, we have different instances of the service that vary in configuration hosted in subdirectories under the root.

I have a Web.Config in the root that has some configuration information in the AppSettings section. Specifically, it looks like this:

  <appSettings>
        <add key="Environment" value="Local" /> 
  </appSettings>

In the different subdirectories, I add another Web.config which adds other settings that are specific to the services in that subdirectory.

<appSettings>
      <add key="Subdir" value="ABC" />
</appSettings>

However, when I invoke a service (.svc) file in the subdirectory, there is only one value in the ConfigurationManager.AppSettings collection ("Environment"). The "Subdir" key is nowhere to be found. (BTW, the code is written using ConfigurationManager, but I've also tried using WebConfigurationManager, and the result is the same.)

The documentation on MSDN clearly says that the Web.config files in nested directories are supposed to be cumulative. So why isn't my "Subdir" key showing up in that collection?

Thanks in advance for any help you can give me.

Upvotes: 0

Views: 1591

Answers (1)

Dave Hanna
Dave Hanna

Reputation: 2521

For the benefit of anyone else that encounters this, I found the answer.

Even though this app is a service running under IIS, and is being accessed through IIS, the point where this config element is needed is deep down in a derived class of System.ServiceModel.ServiceHostFactory. At that point, HttpContext.Current is null, so the ConfigurationManager has no access to Web directory structure information, and has to depend simply on the application configuration, which is at the top level.

So our solution is to make each Subdirectory an IIS application, and give it all of its own config information in one place.

Upvotes: 1

Related Questions