AppSettings in App or Web Config Using a Linked File

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!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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">

Method 2

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

Method 3

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:projectsproj2binDebugproj2.exe";
Configuration proj2Config = ConfigurationManager.OpenExeConfiguration(proj2Exe);
string mysetting = proj2Config .AppSettings.Settings["ThatSetting"].Value;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x