Can't read Web.config with ConfigurationManager.AppSettings - c#

I have build a WCF service that uses Web.config to get some appSettings. In visual studio it works great but when I publish and instal the service it suddenly gets its appSettings from App.config and not from Web.config.
I know this because I loop through appSettings and printed the result to the console with this code:
foreach (string key in ConfigurationManager.AppSettings.AllKeys)
{
Console.WriteLine("Name: {0} Value: {1}", key, ConfigurationManager.AppSettings[key]);
}
My configs look like this:
Web.config
....
<appSettings>
<add key="IQDir" value="C:\Program Files (x86)\Ridder iQ Client (lokaal)\Bin"/>
<add key="FileURL" value="localhost:8080/WebPortal_2.0/"/>
</appSettings>
....
App.config
....
<appSettings>
<add key="test1" value="wtf is going on!"/>
<add key="test2" value="waargh"/>
<add key="test3" value="I am getting a headache over here!!"/>
</appSettings>
....
When I run in visual studio I get:
But when I use the published code inside live environment I get this:
Why is this happening and how can I force ConfigurationManager to get appSettings from Web.config instead of App.config.

If you have a standard WCF-project, you should only have the Web.config-file, not App.config.
I would skip the old way of using appSettings altogether. Use applicationSettings instead, by using the Settings-tab in the project's properties.
It will create this in Web.Config:
<applicationSettings>
<Projectname.Properties.Settings>
<setting name="Testenvironment" serializeAs="String">
<value>True</value>
</setting>
</Projectname.Properties.Settings>
</applicationSettings>
For more information: appSettings vs applicationSettings. appSettings outdated?

configurationManager is used to pick values from the configuration file of project under which it is running. For example, you have exposed your wcf on web server S1 and you are consuming it in a console app from client machine M1. Now if your c# code is running on S1 then it will pick values from web.config from wcf code folder on S1. But if this code is running on client machine M1 (the console app consuming the service), then it will pick values from machine M1. What you are facing, normally happened after publish.

Related

WCF Service web.config not being retrieved via WebConfigurationManager

I'm currently running a WCF service on localhost, with a web.config file that contains credentials to connect to CRM. The web.config file, which is in the root of the project, contains the appSettings as you'd expect:
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<add key="CrmUrl" value="{removed}"/>
<add key="CrmUser" value="{removed}"/>
<add key="CrmPass" value="{removed}"/>
</appSettings>
I then try to retrieve these values with an initialiser
internal CrmOrganizationServiceContext Crm
{
get
{
return CrmConnectionCreator.CreateConnection(
WebConfigurationManager.AppSettings["CrmUrl"],
WebConfigurationManager.AppSettings["CrmUser"],
WebConfigurationManager.AppSettings["CrmPass"]);
}
}
However, null is being passed through to the CreateConnection method. I did a little digging around google, and most suggestions are regarding using WebConfigurationManager, which I currently am doing. Calling WebConfigurationManager.OpenWebConfiguration(null) seems to pass me to a web.config file which exists in the Microsoft.NET framework installation folder, when I'm trying to read in configuration settings from the web.config file in my project.
Is WebConfigurationManager looking in the wrong area, and I have to point it to the right location somehow? I have also tried using plain ConfigurationManager.AppSettings but the results are the same.

Centralised Connection String for multiple application asp.net

I have a web server with multiple application running. All the application have their web.config file. If the database password changes due to Policy I have to manually change the password in each of web.config files in the app setting section.
I was reading about the connection string setting in machine.config file.
Now my question is if I put connection string in appsetting section of machine.config with name ConnectionString and same in my web.config file will it overwrite the machine.config file values.
In my machine.config following is the setting
<configuration>
....
<appSettings>
<add key="ConnectionString" value="value"/>
</appSettings>
</configuration>
similarly in my web.config file
<configuration>
....
<appSettings>
<add key="ConnectionString" value="value"/>
</appSettings>
</configuration>
And I get the value in my code as below
string conString=ConfigurationManager.AppSettings["ConnectionString"];
will I get the overloaded value?
What's going to help you out here is to store your connection string(s) in .config file and then reference them either using the file="" attribute or the configSource="" attribute.
Here's an excellent question and answer that talks about the differences between the two and shows you how to implement them:
ASP.NET web.config: configSource vs. file attributes

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