简体   繁体   English

您将如何在多个项目中集中配置?

[英]How would you centralize configuration across multiple projects?

I have a solution with about 10 projects with read-only config.我有一个包含大约 10 个只读配置项目的解决方案。 They are web applications, windows services, console apps, etc. All projects except for one are on the same server.它们是 Web 应用程序、Windows 服务、控制台应用程序等。除一个之外的所有项目都在同一台服务器上。 Each project has 3 environments - dev, test, and production.每个项目都有 3 个环境 - 开发、测试和生产。 So there are 30 different sets of configuration, each one with a decent number of settings.所以有 30 组不同的配置,每组都有相当数量的设置。 It's cumbersome to keep the config consistent across every app and environment.在每个应用程序和环境中保持配置一致是很麻烦的。

I've noticed most of the configuration is common across each project, so I was thinking it would be good to centralize the config in some way.我注意到大多数配置在每个项目中都是通用的,所以我认为以某种方式集中配置会很好。 I read somewhere that a WCF service might be a good approach.我在某处读到 WCF 服务可能是一种好方法。 I thought maybe a library containing a hard-coded static class might actually work OK - despite having to compile to change config.我想也许一个包含硬编码静态类的库实际上可以正常工作 - 尽管必须编译以更改配置。 Ideally the config should come out of an actual .config file.理想情况下,配置应该来自实际的 .config 文件。

How would you go about centralizing config for multiple projects?您将如何为多个项目集中配置?

If you want to maintain the standard configuration interface, take a look at the ProtectedConfigurationProvider .如果要维护标准配置接口,请查看ProtectedConfigurationProvider This provider lets you store your configuration data outside of a standard configuration file, encrypt it however you like, or redirect requests for configuration in any way you see fit:此提供程序允许您将配置数据存储在标准配置文件之外,根据您的喜好对其进行加密,或以您认为合适的任何方式重定向配置请求:

The beauty of this approach is that nothing changes in your existing applications.这种方法的美妙之处在于您现有的应用程序没有任何变化。 They don't need to know where their configuration is stored.他们不需要知道他们的配置存储在哪里。 The retrieval of configuration data is isolated in the provider.配置数据的检索在提供程序中被隔离。 You can store it in a central file, store it in a database, or access it via a web service.您可以将其存储在中央文件中、存储在数据库中或通过 Web 服务访问它。 If you change your mind, you only have to update your provider.如果您改变主意,您只需更新您的提供商。 Everything else stays the same.其他一切都保持不变。

You could certainly set up a WCF service that has a simple operation to retrieve configuration settings, taking in the application and environment as a parameter;你当然可以设置一个 WCF 服务,它有一个简单的操作来检索配置设置,将应用程序和环境作为参数; you could then have the service load up the correct config from a file and return it to the caller.然后,您可以让服务从文件加载正确的配置并将其返回给调用者。 It might be a good idea to do nested configuration files, so that common settings are only defined once at their most generic level.嵌套配置文件可能是一个好主意,这样公共设置只在最通用的级别定义一次。

A potential issue could arise if the WCF service is down when starting up one of your apps -- you would need to decide if there is default config/caching of the previous copy for this situation, or if you just don't allow apps to start up if they cannot connect.如果 WCF 服务在启动您的一个应用程序时关闭,则可能会出现潜在问题——您需要决定是否存在针对这种情况的先前副本的默认配置/缓存,或者您是否只是不允许应用程序如果无法连接,则启动。

Another thing to consider, though, is the benefit of .config files in .NET in that when they change the app can respond;不过,另一件要考虑的事情是 .NET 中的 .config 文件的好处,因为当它们更改时,应用程序可以响应; you may want to have a callback WCF service that notifies clients if their configuration has been updated on the central server, so they can request a new copy and update themselves if necessary.您可能希望有一个回调 WCF 服务,如果客户端的配置已在中央服务器上更新,则通知客户端,以便他们可以请求新副本并在必要时自行更新。

Since they are (almost) all on the same server you could consider providing defaults in the machine.config and/or central web.config files.由于它们(几乎)都在同一台服务器上,您可以考虑在machine.config和/或中央web.config文件中提供默认值。 I'm not normally a fan of using/changing these file but they are there... in \\Windows\\Micsrosoft.NET\\Framework<version>\\Config\\我通常不喜欢使用/更改这些文件,但它们在那里......在\\Windows\\Micsrosoft.NET\\Framework<version>\\Config\\

You can use a centralized configuration sever like Lygeum which permits to manage applications and environments through web console or Command Line Interface with user management and client management module, clients may be in your case web apps, console services or whatever.您可以使用像Lygeum这样的集中配置服务器,它允许通过 Web 控制台或带有用户管理和客户端管理模块的命令行界面来管理应用程序和环境,在您的情况下,客户端可能是 Web 应用程序、控制台服务或其他任何东西。 The server installation is simple through docker.服务器安装通过docker很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM