web.config transform elements not available through System.Configuration.ConfigurationManager - c#

I have a web.config file that's transformed using a web.debug.config file. Trying to get access to these values through System.Configuration.ConfigurationManager.AppSettings is yielding nothing.
My web.config appSettings are empty.
<configuration>
<appSettings>
</appSettings>
</configuration>
My web.debug.config transform that's applied. Here's a sample.
<configuration>
<appSettings>
<add key="CommonURL" value="localhost/mysite/" xdt:Transform="Insert" />
</appSettings>
</configuration>
Here's the code to try and get this value, which returns null.
var cu = System.Configuration.ConfigurationManager.AppSettings["CommonURL"];
Any idea on why this could be?

Assuming that System.Configuration.ConfigurationManager.AppSettings is yielding nothing when you run from Visual Studio, then that is the expected behavior. You should add the CommonURL appSetting to your web.config file and change the entry in the web.debug.config to:
<add key="CommonURL" value="localhost/mysite/"
xdt:Transform="Replace" xdt:Locator="Match(key)" />
These changes will allow you to get the value specified in the web.config when you run from Visual Studio, and will allow that value to be replaced with what is defined in the web.debug.config when you execute the transform (for example, through the Publish function or through a custom MSBuild script). Keep in mind that the transforms are applied when you publish, not when you start running a debug or release build. The values that are in the web.config file are always the ones that are honored.

Related

c# App.Config file not transforming when building for Test deployment

I am using slow cheetah in my app such that I can have app.config files that will transform in the same manner as web.config transforms. I have used this in other projects with no issue, but for some reason in one of my projects, the cofing file is not changing.
A sample from my App.Test.config is the following:
<add key="timer" value="300" xdt:Locator="Match(key)" xdt:Transform="SetAttributes"/>
And the corresponding line in my App.config file is
<add key="timer" value="15"/>
The problem turned out that I did not include the <AppSettings> tag before the <Add> tag in the transform. So when the transform went to take place it would look for the key timer within <Configuration> and not <Configuration> <AppSettings>

How to set up Web.Config transformation to cater for Local and production configurations?

I have this dilemma of having to have different connection strings for the web.config on my local machine and to have a release transformation that would make the production binaries use the machine.config on the web server.
So I have these files in my Visual Studio solution:
Web.Config
Web.Debug.Config
Web.Release.Config
In the web.config I have removed and added new connection strings.
<remove name="connstring">
<add name="connstring" ConnectionString="blahblah" />
What I want to do is to have nothing in final web.config when deployed (by TFS build) to the web server so that my web application would use anything in the machine.config on the server.
How can I do that?
You can remove configuration settings with the RemoveAll transform attribute. Assuming you are deploying the Release build configuration, you can generate a completely empty web.config by putting the following in Web.Release.Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xdt:Transform="RemoveAll" />
This resulting web.config will have the XML declaration at the top and nothing else.
If you only want to remove certain configuration sub-sections, add the RemoveAll transform attribute to the section you want removed. For example, the following Web.Release.Config will remove all application settings:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings xdt:Transform="RemoveAll" />
</configuration>
See the full Transformation Syntax Documentation for more details.

Making machine.config with multiple external config files

I've been trying to make the machine.config to be clean by adding the connectionString section and appSettings section externally.
It works well with just making the connectionString externally only like below.
Machine.config
<configuration>
<connectionStrings configSource="connectionStrings.config"/>
....
<appSettings>
....
</appSettings>
</configuration>
connectionStrings.config is the same folder where machine.config like below
<connectionStrings>
<add name= .../>
...
</connectionStrings>
But when trying to add the appSettings section as an external config additionally, it seems the application does not read the appSettings section properly. (By debugging under VS, the value of a key in the section does not return anyting and it is empty)
Of course, I tried to both ways when making appSettings external as below:
<appSettings configSource="AppSettings.config"/>
or
<appSettings file="AppSettings.config"/>
so my question is what I am trying to is not possible?
I refer the below article.
http://blog.andreloker.de/post/2008/06/16/Keep-your-config-clean-with-external-config-files.aspx
Here is my working environments:
Windows 7 x64
machine.config that I am working is c:\windows\Microsoft.net\Framework64\v2.0.50727\config\machine.config
The application is x86 build.

Hiding private details from open source projects

I have a .net github project that is basically a wrapper around a web API. In the test project, I am calling to the API using an API key. I need to keep this key private, how do I accomplish this in a visual studio project?
In some other projects, like python, I can have git ignore the file (config.py) and use something like config.example.py. But in visual studio's case, the project will not compile because of the missing file Config.cs. What is the proper way to solve this? I'm thinking of using this same method of ignoring the file and have them execute a build script that should rename Config.example.cs to Config.cs?
This is the perfect for .config files. Depending on whether its a web or console application, you will have a web.config or app.config file in your project.
You can use the appSettings section to store your API key.
To make things even easier, you can actually have this section read from another file, ie: specialappsettings.config and then just ignore that single file from your repository.
Modify your web.config (or app.config):
<configuration>
<appSettings file="specialappsettings.config">
</appSettings>
<system.web>
<!-- standard web settings go here -->
</system.web>
</configuration>
Create a new specialappsettings.config file:
<appSettings>
<add key="APIKey" value="YourApiKeyValue" />
<add key="AnotherKey" value="AnotherValue" />
</appSettings>
This can be accessed in your code via:
var apiKey = ConfigurationManager.AppSettings["APIKey"];
Notes:
You can keep your settings within the original web.config file as
well but this lets you ignore just the specific settings file from
your git repository without affecting the rest of the project's
necessary configuration details.
The same "key" can be saved in
either file however the external file will override the original
web.config file value.
You are probably looking for the App.config file for a project. It will be copied to <application>.exe.config when you compile it. Users can edit that config file as needed.
In that config file, you can add your API keys:
<configuration>
<appSettings>
<add key="APIKey" value="12345"/>
</appSettings>
</configuration>
Then you can access it from your code using ConfigurationManager.AppSettings:
string apiKey = ConfigurationManager.AppSettings["APIKey"];
One option is to use .config files instead of having secret keys hardcoded in sources.
More info Using Settings in C# and step-by-step guide
<configuration>
<appSettings>
<add key="SecretKey" value="0" />
</appSettings>
</configuration>
var secretKey = ConfigurationManager.AppSettings.Get("SecretKey");
Perhaps you can store the key outside of the Config.cs file and load it at run time.
Bonus, other people using your code won't have to recompile the project to change to their API key.

Web.config and different configurations in different projects missmatch

I'm using this code (in my DAL project):
ConfigurationManager.AppSettings["server"]
to access appsettings section in web.config file (from web project):
<appSettings>
<add key="server" value="server.name.com"/>
<add key="database" value="databasename"/>
</appSettings>
and in a Web.Debug.config I'm using a following transformation
<add key="server" value="MY-LAPTOP"
xdt:Locator="Match(key)" xdt:Transform="Replace"/>
after that when I start application the config file isn't transformed. First line of code returns the nontransformed infromation. What's wrong with the code? What am i missing?
I have tried to publish it and when I check config file everything is ok like it is ment to be.
The web.config transformation is only perform during the publish process. You can still enable it on every build, when you it F5, see
ASP.NET Web Projects: web.debug.config & web.release.config
SlowCheetah - XML Transforms
Making Visual Studio 2010 Web.config Transformations Apply on Every Build
It's an MSBuild task to add.

Categories