使用链接文件的 App 或 Web Config 中的 AppSettings [英] AppSettings in App or Web Config Using a Linked File

查看:16
本文介绍了使用链接文件的 App 或 Web Config 中的 AppSettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

此堆栈对类似问题的回答 似乎至少对 configSource 建议此解决方案可行.我不想要 configSource,因为我只想要少数属性在两个项目中是通用的.更新: 我刚试过这个,configSource 也不起作用.它找不到配置文件.这让我相信 common.config 并不总是被视为带有副本的内容.

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.

示例 App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings file="common.config">
    <add key="NotCommonKey" value="1"/>
  </appSettings>
</configuration>

示例 Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings file="common.config">
    <add key="NotCommonKey2" value="2" />
  </appSettings>
</configuration>

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

<appSettings>
  <add key="CommonKey" value="1" />
</appSettings>

我正在使用 ConfigurationManager/WebConfigurationManagerAppSettings 属性中读取数据.

I am using ConfigurationManager / WebConfigurationManager reading from the AppSettings property.

知道为什么当 common.config 是链接文件时,不使用它的 AppSettings 值,而当它没有链接时,它可以正常工作吗?

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?

谢谢!

推荐答案

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

In the Web.Config you must add "bin/" (se example below).

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

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

这篇关于使用链接文件的 App 或 Web Config 中的 AppSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆