AppSettings in App or Web Config Using a Linked File

寵の児 提交于 2019-12-09 02:31:51

问题


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!


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



回答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



来源:https://stackoverflow.com/questions/17437340/appsettings-in-app-or-web-config-using-a-linked-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!