Why machine.config values are not getting picked up?
While working on enterprise applications and otherwise situation arises when you want to share your configurations in machine.config for few of the following reasons:
- Want to share the same configuraiton across multiple websites/applications hosted on the same server.
- To secure configuration values from users who have access to app.config or web.config and not the machine.config
When machine.config is used, sometimes people face problems accessing the machine.config values. This causes delay in the release process. Debugging this mystery is time consuming and sometimes even causes release to be rolled back when the code is already deployed and config is the last thing to be applied.
I have faced the problem when accessing the machine.config values and thought of sharing my experiences . If it so happens then try to debug using the methods mentioned below:
- Clear tag in web.config/app.config - If you are accessing the keys from the configuration file then remember that the sequence for dotnet to access configuration is in the following heirarchy:
- Machine.config - First checked here
- Web.config in the machine.config folder - Consolidated with the entries here(I + ii)
- Web.config of the website - Consolidated with the entries here (I + ii + iii)
- Web.config of the folder in which the file exists - Consolidated with the entries here (I + ii + iii + iv)
The syntax looks like this:
connectionStringName="ABCMembershipDB" applicationName="ABWeb" />
Therefore, if your machine.config values are not getting picked up in your application then check the configuration files in the sequence mentioned above to see if clear tag is present. This could be the cause of the issue.
- 32 bit vs 64 bit machines and .net framework 4 - This applies to windows 64 bit version of operating system. When the machine is 64 bit then dotnet framework creates the following folder during installation:
- c:\Windows\Microsoft.NET\Framework64 - for 64 bit version
- c:\Windows\Microsoft.NET\Framework - for 32 bit version
If during development you have 32 bit version and production has 64 bit version windows operating system installed then you will be surprised that your machine.config values are not getting picked. In machines running 64 bit operating system, updating the machine.config of 32 bit version, will not reflect the changes. So be careful to update appropriate machine.config based on your operating system.
- Load balancer IP same as the production server - This is a rarest of rare situation but it really happened. Sometimes infrastructure team assign the same IP to the load balancer and production server. So when you decide that before rolling out your changes to all the production boxes, you want to add the configuration changes to one box and test before rolling out the machine.config changes to all the servers, then you can be surprised.
Now when you try to access Box1(where you have deployed the machine.config changes)from within the network with the help of IP address(in the above case - IP:22.214.171.124), then you can be in for a surprise. The load balancer will kick in because the IP of the load balancer is being called and it will send the request to any of the boxes. It will be very difficult to understand the random behavior of the request as to when load balancer sends the request to Box1 then everything works but if the request is directed to box 2 then the machine.config value is not picked. Be very careful, understand the infrastructure when you run into situation when machine.config values are not getting picked up.
Hope this helps.
Let me know if you run into any other issues with machine.config and have found solutions no matter how simple(stupid) they may be. Also, please feel free to post me questions if you are stuck with machine.config values not getting picked up.