Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 730
scottheckel Avatar asked Jul 02 '13 23:07

scottheckel


People also ask

What is the appSettings section in the web config file?

The <appSettings> element of a web. config file is a place to store connection strings, server names, file paths, and other miscellaneous settings needed by an application to perform work.

Can we have two Web config files for a Web application?

Yes you can have two web. config files in application. There are situations where your application is divided in to modules and for every module you need separate configuration. For example if you have a application which has two modules lets say accounts and sales.

How do I add a Web config file to my Azure folder?

In the Azure portal, search for and select App Services, and then select your app. In the app's left menu, select Configuration > Default documents. To add a default document, click New document.


1 Answers

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">
like image 192
Daniel Stackenland Avatar answered Sep 24 '22 08:09

Daniel Stackenland