WCF Service web.config not being retrieved via WebConfigurationManager - c#

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.

Related

App Settings from Web.config in WCF Service returning null

I am having an issue retrieving app settings from my web.config file in a WCF service I am creating. When calling my settings, I am retrieving a null value. Digging into the object also shows that there are no keys in AppSettings list.
This issue has a few posts around SO/Google, and I have incorporated all the suggestions, but to no avail. Here is my current position and what I have tried:
web.config:
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<!-- CRM Creds -->
<add key="CrmUrl" value="[removed]"/>
<add key="CrmUser" value="[removed]"/>
<add key="CrmPass" value="[removed]"/>
</appSettings>...
Methods of calling the settings:
System.Web.Configuration.WebConfigurationManager.AppSettings["CrmUrl"]
System.Configuration.ConfigurationManager.AppSettings["CrmUrl"]
The file I am calling from is within a folder, whereas web.config is in the root. I understand WebConfigurationManager could find this regardless, but I am having no luck.
Code:
%projectfolder%/Client/Client.cs
Config: %projectfolder%/web.config
I have run out of things to try and have used up all my Google-fu. Any suggestions are appreciated.

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

Getting ConnectionString of a BizTalk config file from a C# Application

I have a BizTalk config file that I want to use in my C# application. I'd like to get the connection string from the BizTalk config. Is there a way to do this? Simply put, I want to read a connection string from an external config file.
What I'm currently using in my C# app config is:
<configuration>
<appSettings>
<add key="foo" value="blah;"/>
<add key="foo" value="blah;"/>
</appSettings>
</configuration>
I get the keys by using this code:
connectionString = ConfigurationManager.AppSettings[configKey];
Thanks.
From an external .config file, as in not the current .exe's config file, no.
System.Configuration will always refer to the local .config.
To access another .exe's .config file, you have to treat it as just an Xml file with System.Xml.

Can't read Web.config with ConfigurationManager.AppSettings

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.

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.

Categories