简体   繁体   English

了解.Net配置选项

[英]Understanding .Net Configuration Options

I'm really confused by the various configuration options for .Net configuration of dll's, ASP.net websites etc in .Net v2 - especially when considering the impact of a config file at the UI / end-user end of the chain. 我对.Net v2中dll,ASP.net网站等的.Net配置的各种配置选项感到困惑 - 尤其是在考虑配置文件在链接的UI /最终用户端的影响时。

So, for example, some of the applications I work with use settings which we access with: 因此,例如,我使用的一些应用程序使用我们访问的设置:

string blah = AppLib.Properties.Settings.Default.TemplatePath;

Now, this option seems cool because the members are stongly typed, and I won't be able to type in a property name that doesn't exist in the Visual Studio 2005 IDE. 现在,这个选项似乎很酷,因为成员是强力键入的,我将无法输入Visual Studio 2005 IDE中不存在的属性名称。 We end up with lines like this in the App.Config of a command-line executable project: 我们在命令行可执行项目的App.Config中得到这样的行:

<connectionStrings>
    <add name="AppConnectionString" connectionString="XXXX" />
    <add name="AppLib.Properties.Settings.AppConnectionString" connectionString="XXXX" />
</connectionStrings>

(If we don't have the second setting, someone releasing a debug dll to the live box could have built with the debug connection string embedded in it - eek) (如果我们没有第二个设置,那么将一个调试dll发布到live box的人可能已经嵌入了调试连接字符串 - eek)

We also have settings accessed like this: 我们也有像这样访问的设置:

string blah = System.Configuration.ConfigurationManager.AppSettings["TemplatePath_PDF"];

Now, these seem cool because we can access the setting from the dll code, or the exe / aspx code, and all we need in the Web or App.config is: 现在,这些看起来很酷,因为我们可以从dll代码或exe / aspx代码访问设置,我们在Web或App.config中需要的只是:

<appSettings>
   <add key="TemplatePath_PDF" value="xxx"/>
</appSettings>

However, the value of course may not be set in the config files, or the string name may be mistyped, and so we have a different set of problems. 但是,当然可能没有在配置文件中设置值,或者字符串名称可能输入错误,因此我们遇到了一组不同的问题。

So... if my understanding is correct, the former methods give strong typing but bad sharing of values between the dll and other projects. 所以......如果我的理解是正确的,那么前面的方法会给出强大的输入,但是在dll和其他项目之间分配不好的值。 The latter provides better sharing, but weaker typing. 后者提供更好的共享,但键入较弱。

I feel like I must be missing something. 我觉得我必须遗漏一些东西。 For the moment, I'm not even concerned with the application being able to write-back values to the configuration files, encryption or anything like that. 目前,我甚至不关心应用程序能够将值写回配置文件,加密或类似的东西。 Also, I had decided that the best way to store any non-connection strings was in the DB... and then the very next thing that I have to do is store phone numbers to text people in case of DB connection issues, so they must be stored outside the DB! 此外,我已经决定存储任何非连接字符串的最佳方法是在数据库中...然后我要做的另一件事就是在发生数据库连接问题时将电话号码存储给文本人员,所以他们必须存放在DB外面!

If you use the settings tab in VS 2005+, you can add strongly typed settings and get intellisense, such as in your first example. 如果您使用VS 2005+中的设置选项卡,则可以添加强类型设置并获取智能感知,例如在您的第一个示例中。

string phoneNum = Properties.Settings.Default.EmergencyPhoneNumber;

This is physically stored in App.Config. 它实际存储在App.Config中。

You could still use the config file's appSettings element, or even roll your own ConfigurationElementCollection, ConfigurationElement, and ConfigurationSection subclasses. 您仍然可以使用配置文件的appSettings元素,甚至可以使用自己的ConfigurationElementCollection,ConfigurationElement和ConfigurationSection子类。

As to where to store your settings, database or config file, in the case of non-connection strings: It depends on your application architecture. 至于存储设置,数据库或配置文件的位置,对于非连接字符串:它取决于您的应用程序体系结构。 If you've got an application server that is shared by all the clients, use the aforementioned method, in App.Config on the app server. 如果您有一个由所有客户端共享的应用程序服务器,请在应用程序服务器上的App.Config中使用上述方法。 Otherwise, you may have to use a database. 否则,您可能必须使用数据库。 Placing it in the App.Config on each client will cause versioning/deployment headaches. 将其放在每个客户端的App.Config中将导致版本控制/部署问题。

Nij, our difference in thinking comes from our different perspectives. Nij,我们的思维差异来自于我们不同的观点。 I'm thinking about developing enterprise apps that predominantly use WinForms clients. 我正在考虑开发主要使用WinForms客户端的企业应用程序。 In this instance the business logic is contained on an application server. 在这种情况下,业务逻辑包含在应用程序服务器上。 Each client would need to know the phone number to dial, but placing it in the App.config of each client poses a problem if that phone number changes. 每个客户端都需要知道要拨打的电话号码,但是如果该电话号码发生变化,将其放在每个客户端的App.config中就会出现问题。 In that case it seems obvious to store application configuration information (or application wide settings) in a database and have each client read the settings from there. 在这种情况下,将应用程序配置信息(或应用程序范围设置)存储在数据库中并让每个客户端从那里读取设置似乎是显而易见的。

The other, .NET way, (I make the distinction because we have, in the pre .NET days, stored application settings in DB tables) is to store application settings in the app.config file and access via way of the generated Settings class. 另一种是.NET方式(我之所以区别,因为我们在.NET之前的日子里,在DB表中存储了应用程序设置)是将应用程序设置存储在app.config文件中,并通过生成的Settings类进行访问。

I digress. 我离题了。 Your situation sounds different. 你的情况听起来不一样 If all different apps are on the same server, you could place the settings in a web.config at a higher level. 如果所有不同的应用程序都在同一台服务器上,您可以将设置放在更高级别的web.config中。 However if they are not, you could also have a seperate "configuration service" that all three applications talk to get their shared settings. 但是,如果不是这样,您还可以拥有一个单独的“配置服务”,所有三个应用程序都可以通过它来获取共享设置。 At least in this solution you're not replicating the code in three places, raising the potential of maintenance problems when adding settings. 至少在此解决方案中,您不会在三个位置复制代码,从而在添加设置时增加了维护问题的可能性。 Sounds a bit over engineered though. 听起来有点过于设计。

My personal preference is to use strong typed settings. 我个人的偏好是使用强类型设置。 I actually generate my own strongly typed settings class based on what it's my settings table in the database. 我实际上是根据它在数据库中的设置表生成我自己的强类型设置类。 That way I can have the best of both worlds. 这样我就能拥有两全其美。 Intellisense to my settings and settings stored in the db (note: that's in the case where there's no app server). 智能感知我存储在数据库中的设置和设置(注意:在没有应用服务器的情况下)。

I'm interested in learning other peoples strategies for this too :) 我也有兴趣学习其他人的策略:)

I think your confusion comes from the fact that it looks like your first example is a home-brewed library, not part of .NET. 我认为你的困惑来自这样一个事实:看起来你的第一个例子是家庭酿造的库,而不是.NET的一部分。 The configurationmanager example is an example of built-in functionality. 配置管理器示例是内置功能的示例。

I support Rob Grays answer, but wanted to add to it slightly. 我支持Rob Grays的回答,但想稍微补充一下。 This may be overly obvious, but if you are using multiple clients, the app.config should store all settings that are installation specific and the database should store pretty much everything else. 这可能过于明显,但如果您使用多个客户端,则app.config应存储特定于安装的所有设置,并且数据库应该存储几乎所有其他设置。

Single client (or server) apps are somewhat different. 单个客户端(或服务器)应用程序有些不同。 Here it is more personal choice really. 这里真的是个人选择。 A noticable exception would be if the setting is the ID of a record in the database, in which case I would always store the setting in the database with a foreign key to ensure the reference doesn't get deleted. 一个值得注意的例外是,如果设置是数据库中记录的ID,在这种情况下,我将始终使用外键将设置存储数据库中,以确保不会删除引用。

Yes - I think I / we are in the headache situation Rob descibes - we have something like 5 or 6 different web-sites and applications across three independent servers that need to access the same DB. 是的 - 我认为我/我们处于头疼状态Rob descibes - 我们在需要访问同一个数据库的三个独立服务器上有5个或6个不同的网站和应用程序。 As things stand, each one has its own Web or App.config with the settings described setting and / or overriding settings in our main DB-access dll library. 事实上,每个人都有自己的Web或App.config,其设置描述了我们的主DB-access dll库中的设置和/或覆盖设置。

Rob - when you say application server, I'm not sure what you mean? Rob - 当你说应用程序服务器时,我不确定你的意思? The nearest thing I can think is that we could at least share some settings between sites on the same machine by putting them in a web.config higher in the directory hierarchy... but this too is not something I've been able to investigate... having thought it more important to understand which of the strong or weak-typed routes is 'better'. 我能想到的最接近的事情是,我们至少可以在同一台机器上的站点之间共享一些设置,方法是将它们放在目录层次结构中较高的web.config中......但这也不是我能够调查的内容...认为更重要的是要了解哪种强弱路线是“更好”。

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

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