views:

892

answers:

3

Hi all,

This problem has several of us stumped in the office. We are all new to deploying ASP.NET apps to a web farm, and I am fresh out of ideas.

We have a web farm, and the application is copied on to all of them. However, we are having a problem..

An exception is being thrown when trying to get settings from appSettings. Upon further investigation, it turns out the node is actually not using the local Web.Config but it falling back to the Web.Config in the .NET framework folder (we have proved this by adding keys there, which appear on a test page).

I must be missing something, because my understanding is that so long as the file is there, IIS should use that! One of the servers seems to work fine!

Here's a list of what we have confirmed:

  • The config file is in the app directory.
  • Said file's content is correct.
  • When viewing the file from IIS > Site > Properties > ASP.NET > Edit Config the correct content is shown.

However, at run-time the file that is used is the global one (windows\ms .net\framework\v2\config\web.config).

Anyone have any suggestions as to what may be going wrong? Appreciate all the help I can get!

Thanks.

Rob

+3  A: 

This is the hierarchy for ASP.NET configuration. Maybe this could help understanding which settings overwrite each other.

Server

Machine.config: The Machine.config file contains the ASP.NET schema for all of the Web applications on the server. This file is at the top of the configuration merge hierarchy.

Root Web

Web.config: The Web.config file for the server is stored in the same directory as the Machine.config file and contains default values for most of the system.web configuration sections. At run time, this file is merged second from the top in the configuration hierarchy.

Web site

Web.config: The Web.config file for a specific Web site contains settings that apply to the Web site and inherit downward through all of the ASP.NET applications and subdirectories of the site.

ASP.NET application root directory

Web.config: The Web.config file for a specific ASP.NET application is located in the root directory of the application and contains settings that apply to the Web application and inherit downward through all of the subdirectories in its branch.

ASP.NET application subdirectory

Web.config: The Web.config file for an application subdirectory contains settings that apply to this subdirectory and inherit downward through all of the subdirectories in its branch.

Client application directory

ApplicationName.config: The ApplicationName.config file contains settings for a Windows client application (not a Web application).


Understanding which ASP.NET files and folders are inherited through folders and applications is very important for development and troubleshooting.

Here is a short summary:

  • web.config files inherit all the way down the tree, past all application boundaries.
  • global.asax only lives within its application
  • /bin and the /app_{folders} only live within their application

So, this means that anything set in the root web.config file will inherit down the entire site, even if some folders are marked as applications.

Where this gets messy is if the web.config file has references to assemblies but sub-applications don't have those assemblies. For example, let's say that you have a HTTP Module configured in the root of the site and referenced from the site web.config file. If you have a sub-application called /subfolder which is marked as an application, then /subfolder will attempt to load the HTTP Handler from /subfolder/bin. Since it doesn't exist, an error will be thrown.

There are multiple ways around this. Probably the cleanest if the HTTP Handler isn't needed in /subfolder is by 'removing' the reference by adding a clause in the /subfolder/web.config file. You can do this with . Here is an example of how to remove a HTTP Module in a subfolder:

<httpModules>
  <remove name="ErrorLog"/>
</httpModules>

Here is what the site web.config might look like:

<httpModules>
      <add name="ErrorLog"  type="GotDotNet.Elmah.ErrorLogModule, GotDotNet.Elmah, Version=1.0.5527.0, Culture=neutral, PublicKeyToken=978d5e1bd64b33e5" />
</httpModules>print("code sample");
splattne
My understanding is that the application level file takes precedence? The problem here is that at runtime, the application file does not get picked up. Thanks for the reply.
Rob Cooper
Based on your expansion [thanks] - the file being applied is the Root Web, but there *is* a Web.config for the application!
Rob Cooper
What happens, if you insert an erroroneous entry in that web.config? Does the application get that?
splattne
A: 

From what you tell in the comments, I suggest going with "Try and Error".

What happens, if you insert an erroneous entry by purpose? Does the application fail or doesn't that affect anything?

Try to copy the content and then delete and recreate that file from scratch.

splattne
A: 

Hi All,

First off, thank you very much to the guys that answered, I do appreciate the help!

Just an update on this issue. It was a tough one!

Turns out, there was nothing wrong with the code, or the configuration.

It appears that something weird was going on with the server farm (which I have absolutely no control over or access to). The sysadmin re-built the farm, re-deployed the solution and all worked fine.

I guess we will never know what was wrong, but at least we know it was not a development issue!

Thanks again, Rob

Rob Cooper