AppSettings in App or Web Config Using a Linked File - c#

I'm trying to reference some common config settings between a Windows Service and an ASP.NET MVC website. I am doing this by using the file attribute on appSettings in either the App.config or Web.config (respectively). The file (named common.config) that is being referenced is a linked file in a separate project in the same solution. That common.config is set to Content with Copy Always in both projects.
This stack answer to a similiar question seems to suggest at least for configSource this solution would work. I don't want configSource though as I only want a handful of the properties to be common amongst the two projects. Update: I just tried this, and the configSource also doesn't work. It can't find the config file. This leads me to believe the common.config is not treated as content with copy always.
Example App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="common.config">
<add key="NotCommonKey" value="1"/>
</appSettings>
</configuration>
Example Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings file="common.config">
<add key="NotCommonKey2" value="2" />
</appSettings>
</configuration>
Example common.config (Content -> Copy Always)
<appSettings>
<add key="CommonKey" value="1" />
</appSettings>
I am using ConfigurationManager / WebConfigurationManager reading from the AppSettings property.
Any ideas why when the common.config is a linked file, it's AppSettings values are not used and when it is not linked it works as normal?
Thanks!

In the Web.Config you must add "bin/" (se example below).
By default the web.config is NOT copied into the bin folder but the file common.config is, therefore you must add the path from web.config. In a non-web project the default behavior is that the App.config is copied to the bin folder with name MyProgram.exe.config and is in the same directory as common.config.
<appSettings file="bin/common.config">

The idea of using "bin/..." is good but leads to an error saying that "/" is an invalid character in the resulting virtual path.
The proper solution is tu use "bin...".
Cheers

I use this to access another .exe's config file, not sure whether it will work with a MVC project, but this might get you closer:
string proj2Exe = #"C:\projects\proj2\bin\Debug\proj2.exe";
Configuration proj2Config = ConfigurationManager.OpenExeConfiguration(proj2Exe);
string mysetting = proj2Config .AppSettings.Settings["ThatSetting"].Value;

Related

App.config file not getting read from non-startup project

In my solution I have two projects, one which is set to the startup project, the other not. I have added an App.config file to the start-up project.
The App.config file works in the project which is the startup project, but I cannot read the App.config from the other project, the data is coming back as null.
If I want my non start-up project to access the App.config file in the start-up project, what settings do I need to change?
I am using the following code to access the file. String testrail is coming back as null when running from the non start-up project.
public string testrail;
testrail = ConfigurationManager.AppSettings["testrail"];
My App.config file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="testrail" value="True"/>
</appSettings>
</configuration>

Share config file with multiple project in same solution

My project use Entity Framework 6 Database First for the DAL, and it need to have a connectionString in every project's config file.
As I have multi project in solution, I found that it can be a problem if I need to modify connection string, which I need to check every config file.
So I come up an idea, create a Share config file under the solution directory.
The structure:
+ Solution
- CommonConfig
- Share.Debug.config
- Share.Release.config
- Project1
- web.config
- web.Debug.config
- web.Release.config
- Share.Debug.config (Add with Link)
- Share.Release.config (Add with Link)
- Project2
...
And I have move the connectionString section to Share.xxx.config
<connectionStrings>
<add name="db" connectionString="..." />
</connectionStrings>
First test with configSource, it work without problem
<?xml version="1.0" encoding="utf-8"?>
<configuration>
...
<connectionStrings configSource="bin\Share.Debug.config"/>
...
</configuration>
Then I have remove whole section for connectionString
And modify the web.Debug.config
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings configSource="bin\Share.Debug.config" xdt:Transform="InsertIfMissing" />
<system.web>
</system.web>
</configuration>
But it doesn't work, the EF cannot find the connectionString with this setting.
Will it a problem if I want to add a whole section instead of a node?
EDIT
After read several post, some said the transform only work when I publish the website, so I try to publish it to my local IIS.
When I create and preview the publish profile, it return error
A section using 'configSource' may contain no other attributes or elements.

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.

Can I store settings in a settings.config file in ASP.NET MVC?

I'm using .NET MVC
I have about 10 properties I want to store in a configuration file (.config etc.), related to environment/deployment stuff, + other things for quick changes without doing dLL deploys.
I'm using Team foundation service for CI builds etc, and my web.config is obviously under version-contrl.
What I'd like to do is have a settings.config (that's not in version control) file to store these, am I able to do this?
Or does it need to be in web.config?
To answer the title question, yes you can store settings in a separate config file, to do so you need to define the configSource property of appSettings element
E.g.
<appSettings configSource="settings.config" />
and in the settings.config file
<?xml version="1.0" encoding="UTF-8"?>
<appSettings>
<add key="settingKey" value="environmentValue" />
</appSettings>
However, for the sake of environment specific settings, you may want to look at config transforms. Setting up a transform config for each environment then deploying to that environment with the specified build configuration.
E.g. Web.Dev.config (provided you have setup a 'Dev' build configuration)
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="settingKey"
value="devEnvironmentValue"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
</configuration>
More details of build configuration and config transforms here: http://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx
Or you could take advantage of TFS features and parameterize the environment variables, I don't have a lot of experience with this, but the following should help: http://ig.obsglobal.com/2013/02/tfs-and-continuous-deployment-part-4-parameterized-deployments/

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