使用链接文件在App或Web配置中进行AppSettings

sco*_*kel 13 .net c# asp.net app-config web-config

我正在尝试在Windows服务和ASP.NET MVC网站之间引用一些常见的配置设置.我是通过在App.config或Web.config(分别)中使用appSettings上的file属性来完成的.正在引用的文件(名为common.config)是同一解决方案中单独项目中的链接文件.在两个项目中,common.config都设置为Content with Copy Always.

这个堆栈回答一个类似的问题似乎表明至少对于configSource这个解决方案是可行的.我不想要configSource,因为我只希望在两个项目中共享一些属性.更新:我刚试过这个,而且configSource也不起作用.它找不到配置文件.这让我相信common.config不会被视为带有副本的内容.

示例App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings file="common.config">
    <add key="NotCommonKey" value="1"/>
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

示例Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings file="common.config">
    <add key="NotCommonKey2" value="2" />
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

示例common.config(内容 - >始终复制)

<appSettings>
  <add key="CommonKey" value="1" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

我正在使用从AppSettings属性读取的ConfigurationManager/WebConfigurationManager.

任何想法为什么当common.config是一个链接文件,它的AppSettings值没有使用,当它没有链接时,它正常工作?

谢谢!

Dan*_*and 12

在Web.Config中,您必须添加"bin /"(下面的示例).

默认情况下,web.config不会复制到bin文件夹,但是文件common.config是,因此您必须从web.config添加路径.在非Web项目中,默认行为是将App.config复制到名为MyProgram.exe.config的bin文件夹,并且与common.config位于同一目录中.

<appSettings file="bin/common.config">
Run Code Online (Sandbox Code Playgroud)

  • 我想你错过了我的理解 - 我的观点是web.config没有被复制到bin文件夹中,但是文件common.config是,因此你必须从web.config添加路径.在非Web项目中,默认行为是将app.config复制到名为MyProgram.exe.config的bin文件夹,并且与common.config位于同一目录中 (2认同)