在.NET共享配置文件配置文件、NET

2023-09-04 02:12:20 作者:离殇

我有一个解决方案,包括网络和Windows NT服务应用程序。这些当然是两个不同的项目,但相同的解决方案中。然而他们分享了很多相同的配置。

I have a solution that includes both a web and a Windows NT service application. These are of course two different projects but within the same solution. They do however share a lot of the same configuration.

目前我有两个在web.config和app.config文件相同的价值观。这是开始变得混乱,我想有一个共享的配置文件,这两个解决方案中的应用程序。

Currently I have the same values in both the web.config and the app.config file. This is starting to get messy and I'd like to have a shared configuration files for both of the applications within the solution.

是否有相关网站的问题 如果应用程序的配置 是不是在网络的根级别 应用?是否有限制吗? 我会失去缓存和 自动回收纸幅的 应用程序,如果我不使用的web.config 是它通常是一个坏主意,共享配置的描述? Is there a problem for the web application if the configuration isn't at the root level of the web application? Are there limitations here? Will I loose caching and automatic recycling of the web application if I don't use web.config Is it generally a bad idea to shared the configuration as described?

推荐答案

那么,你可以外部化的配置的成单独的.config文件的某些部分,并利用它们从两个地方。

Well, you could "externalize" certain parts of the config's into separate .config files, and use them from both places.

例如。你可以外部化您的连接字符串的设置,像这样:

E.g. you could externalize your connection string settings like so:

<connectionStrings configSource="connectionStrings.config" />

再有connectionString.config文件是这样的:

and then have the "connectionString.config" file like this:

<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
  <add name="ConfigurationDatabase" 
       connectionString="server=.;Integrated Security=true;database=test"/>
  <add name="TestDatabase" 
       connectionString="server=TEST;Integrated Security=true;database=test"/>
</connectionStrings>

基本上,任何配置节有这样的configSource的设置,允许你指定一个外部文件来使用。

Basically, any ConfigurationSection has such a "configSource" setting, which allows you to specify an external file to use.

这样的话,你也许可以共享这两个配置文件的公用部分。

That way, you might be able to share common parts of the two config files.

马克·

 
精彩推荐
图片推荐