跨解决方案共享应用程序配置

发布于 2024-08-21 09:08:04 字数 1044 浏览 2 评论 0原文

我有一个具有多个输出项目的解决方案(一个网站、管理工具和一个 SOAP API 层)。

它们各自共享解决方案中的公共项目(服务层、数据层等)。在这些常见项目之一中,我希望存储一个配置层。

现在,我们为每个输出项目提供了三个单独的 appsettings 配置文件 -

  • development.AppSettings.config
  • testing.AppSettings.config
  • development.AppSettings.config >

所以总共有九个配置文件。每个项目中仅使用一个,因为它们是通过利用 configSource 属性。

无论如何,每当我们想要从配置文件中添加/删除值时,这都会变得很痛苦,因为这意味着我们必须更改所有九个文件才能做到这一点。这就是我想做的:

在公共项目中,我们有三个配置文件,如上所述。这些将被设置为复制到输出目录,以便每个项目都有它们的副本。这些将是“基本”配置。

然后在每个项目中,我想再次拥​​有三个文件,但它们不一定必须包含与基本配置相同的值。但是,如果他们这样做了,那么基本配置值将被输出项目配置中的值覆盖。我想是配置继承的一种形式。

在应用程序启动时,我希望能够获取这两个配置文件 - 基本配置和项目配置文件。然后相应地设置应用程序设置。

但我想知道,确定要使用哪个文件的好方法是什么?另外,我想知道这是否是在大型解决方案中共享应用程序值的好方法,以及是否还有其他更有效的方法?

如果我处于开发模式,那么我不需要 Production.appsettings.config,如果我处于生产模式,反之亦然。

在我离开并获取配置之前,有没有一种简单的方法可以获取我所处的模式(开发/测试/生产)?

I have a solution which has multiple output projects (a website, and admin tool, and a SOAP API layer).

They each share common projects in the solution (the service layer, data layer etc). In one of these common projects, I am looking to store a config layer.

Right now, we have three seperate appsettings config files for each output project -

  • development.AppSettings.config
  • testing.AppSettings.config
  • production.AppSettings.config

So altogether, there are nine config files. Only one is used in each project, as they are referenced by utilising the configSource attribute in the web.config appsettings node.

Anyhoo, it's getting to be a pain any time we want to add/remove values from our config files, because it means that we have to change all nine files to do this. And here's what I'd like to do:

In the common project, we have three config files as above. These would be set to copy to the output directory, so that each project has a copy of them. These would be the 'base' config.

Then in each project, I would like to have three files again, but they wouldn't necessarily have to contain the same values as the base configs. If they did however, then the base config value would be overridden by the value in the output project config. A form of configuration inheritance, I suppose.

On application start, I'd like to be able to get these two config files - the base config, and the project config file. And then set the app settings accordingly.

What I'm wondering though, is what's a nice way of determining which file to use? Also, I'm wondering if this is a good way of sharing application values across a large solution, and if there's another, perhaps more efficient way of doing it?

If I'm in development mode, then I don't want production.appsettings.config, and vice versa if I'm in production mode.

Is there a simple way to get the mode (development/testing/production) that I'm in before I go off and get the configurations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的往事 2024-08-28 09:08:04

您可以拥有一组文件(3 个配置)并在您需要的任何项目中链接/共享它们。

http://www.devx.com/vb2themax/Tip/18855

希望这有帮助。

You can have one set of files (3 configs) and link/share them in whatever projects you need.

http://www.devx.com/vb2themax/Tip/18855

Hope this helps.

橘和柠 2024-08-28 09:08:04

您可以使用 ConfigurationManager.OpenExeConfiguration 静态方法。这将允许您使用任意数量的配置文件。

您还可以尝试创建自定义类来存储所有设置。然后,您可以序列化对象以将其保存为文件。您可以扩展您的基本自定义配置类以适应您的所有其他项目。

You could use the ConfigurationManager.OpenExeConfiguration static method. This will allow you to work with as many config files as you want.

You may also try creating a custom class to store all of your settings. You could then serialize your object to save it as a file. You could extend your base custom config class to fit all your other projects.

稍尽春風 2024-08-28 09:08:04

经过深思熟虑,并在 03:30 去了趟厕所,我找到了一个可行的解决方案。

假设我们的基本配置文件中有一些 appSettings:

<add key="MyKey1" value="MyValue1" />
<add key="MyKey2" value="MyValue2" />
<!-- And so on... -->
<add key="MyKey5" value="MyValue5" />

在我的输出项目中,我有三个 appSettings:

<!-- This is used to identify which config to use. -->
<add key="Config" value="Development" />

<!-- Different value to the one in the base -->
<add key="MyKey2" value="NewValue2" />

<!-- This key does not exist in the base config -->
<add key="MyKey6" value="MyValue6" />

在我的 Application_Start 中,我调用了 GetConfigs()

ConfigHelper.GetConfig(HostingEnvironment. MapPath(“~/bin/BaseConfig”));

实际的 GetConfigs 函数:

public static void GetConfigs()
{
  if (configMode == null)
  {
    configMode = ConfigurationManager.AppSettings.Get("Config").ToLowerInvariant();
  }

  //Now load the app settings file and retrieve all the config values.
  var config = XElement.Load(@"{0}\AppSettings.{1}.config".FormatWith(directory, configMode))
    .Elements("add")
    .Select(x => new { Key = x.Attribute("key").Value, Value = x.Attribute("value").Value })
    //If the current application instance does not contain this key in the config, then add it.
    //This way, we create a form of configuration inheritance.
    .Where(x => ConfigurationManager.AppSettings.Get(x.Key) == null);

  foreach (var configSetting in config)
  {
      ConfigurationManager.AppSettings.Set(configSetting.Key, configSetting.Value);
  }
}

现在,我的输出项目实际上具有以下配置设置:

<add key="Config" value="Development" />
<add key="MyKey1" value="MyValue1" />
<add key="MyKey2" value="NewValue2" />
<!-- And so on... -->
<add key="MyKey5" value="MyValue5" />
<add key="MyKey6" value="MyValue6" />

简单!

After some careful thought, and a trip to the toilet at 03:30, I came across a solution which works.

Let's say that we have some appSettings in our base config file:

<add key="MyKey1" value="MyValue1" />
<add key="MyKey2" value="MyValue2" />
<!-- And so on... -->
<add key="MyKey5" value="MyValue5" />

And in my output project, I have three appSettings:

<!-- This is used to identify which config to use. -->
<add key="Config" value="Development" />

<!-- Different value to the one in the base -->
<add key="MyKey2" value="NewValue2" />

<!-- This key does not exist in the base config -->
<add key="MyKey6" value="MyValue6" />

In my Application_Start, I have a call to GetConfigs():

ConfigHelper.GetConfig(HostingEnvironment.MapPath("~/bin/BaseConfig"));

And the actual GetConfigs function:

public static void GetConfigs()
{
  if (configMode == null)
  {
    configMode = ConfigurationManager.AppSettings.Get("Config").ToLowerInvariant();
  }

  //Now load the app settings file and retrieve all the config values.
  var config = XElement.Load(@"{0}\AppSettings.{1}.config".FormatWith(directory, configMode))
    .Elements("add")
    .Select(x => new { Key = x.Attribute("key").Value, Value = x.Attribute("value").Value })
    //If the current application instance does not contain this key in the config, then add it.
    //This way, we create a form of configuration inheritance.
    .Where(x => ConfigurationManager.AppSettings.Get(x.Key) == null);

  foreach (var configSetting in config)
  {
      ConfigurationManager.AppSettings.Set(configSetting.Key, configSetting.Value);
  }
}

Now, my output project effectively has the following configuration settings:

<add key="Config" value="Development" />
<add key="MyKey1" value="MyValue1" />
<add key="MyKey2" value="NewValue2" />
<!-- And so on... -->
<add key="MyKey5" value="MyValue5" />
<add key="MyKey6" value="MyValue6" />

Simples!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文