AppSettings in App or Web Config Using a Linked File
Asked Answered
L

3

14

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!

Landman answered 2/7, 2013 at 23:18 Comment(3)
1. Are you sure that common.config is in the same folder as your other config files? 2. Any changes to the common.config won't be reflected in your app, until you restart.Gettogether
1. I actually do think that's the issue. I think for some reason when I run the debug it's not copying the linked common.config into the web app/console app folder even though it is set to copy the file.Landman
I noticed that if I create a console app that uses a linked config file that resides in another project it does work. It must be specific to web.config files. I've created an issue for this on CodePlex aspnet.codeplex.com/workitem/10351Landman
L
15

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">
Lenka answered 13/3, 2014 at 20:59 Comment(5)
Wrong - the only case where the configuration file is copied to the bin folder is if the web.config is marked with the build action of "compile". In which case, it can be given the assembly name but with the extension "config". i.e. MyProgram.dll.configLipp
I think you missunderstood me - my Point is that 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.configLenka
I'm accepting the answer. I don't have the project anymore, but this seems right to me. Not entirely sure why I didn't look in this folder for the file in the first place. Thanks.Landman
I can confirm this answer is correct, thanks @DanielStackenlandPerth
This is the best answer that I've seen. Other answers to similar questions have you create a post-build MSBuild task, which just seems wrong. This fixes the issues for debugging and during production builds, without the need for additional, potentially forgotten steps.Alisonalissa
R
0

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

Remanence answered 14/8, 2019 at 9:5 Comment(1)
Could you explain a little bit more please?Michealmicheil
T
0

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;
Throstle answered 19/12, 2019 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.