简体   繁体   English

条件.NET应用程序设置

[英]Conditional .NET application settings

I have a client-server application, where there two options for the server - standalone executable or in another AppDomain in the client, which is very convenient for the debugging purposes. 我有一个客户端 - 服务器应用程序,其中有两个服务器选项 - 独立可执行文件或客户端中的另一个AppDomain,这非常方便用于调试目的。 The choice of the server is transparent to the client, the only file that needs to be changed is the client app.config. 服务器的选择对客户端是透明的,唯一需要更改的文件是客户端app.config。

In order to switch from the standalone server to the local one, some sections need to be un-commented (like nhibernate configuration, which is only relevant to the server) along with individual settings (like authentication implementation, which is again only relevant to the server). 为了从独立服务器切换到本地服务器,需要取消注释某些部分(如nhibernate配置,这只与服务器相关)以及各个设置(如身份验证实现,再次仅与服务器)。

As of now, switching between the two modes is tedious and error prone, because one has to (un)comment several sections and individual settings. 截至目前,在两种模式之间切换是繁琐且容易出错的,因为必须(取消)对几个部分和个别设置进行注释。

Is there a way to specify conditional settings/sections in app.config? 有没有办法在app.config中指定条件设置/部分? Or maybe there is a way to include another config file in the app.config? 或者也许有一种方法可以在app.config中包含另一个配置文件? This way we could put all the local server specific settings in another file and only (un)comment its inclusion. 通过这种方式,我们可以将所有本地服务器特定设置放在另一个文件中,并且仅(un)注释其包含。

I would like to stress the fact, that I wish to have conditional sections , in addition to application settings. 我想强调的是,除了应用程序设置之外,我希望有条件部分

Final note. 最后的说明。 The described scenario is obviously not for production. 所描述的场景显然不适合生产。 It is used exclusively for running unit tests. 它专门用于运行单元测试。 We use mstest for our unit tests. 我们使用mstest进行单元测试。

Thanks. 谢谢。

In ASP.NET it is possible to have some settings in an external file (but I'm not sure if this is also available in windows apps): 在ASP.NET中,可以在外部文件中进行一些设置(但我不确定它是否也可以在Windows应用程序中使用):

Update : this also works for console/winforms applications. 更新 :这也适用于控制台/ winforms应用程序。

web.config/app.config: 的web.config /的app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!-- comment one of the following two lines -->

  <!--appSettings-->
  <appSettings file="YourSettings.config">

    <add key="KeyToOverride" value="Original" />
    <add key="KeyToNotOverride" value="Standard" />

  </appSettings>
</configuration>

YourSettings.config: YourSettings.config:

<appSettings>
  <add key="KeyToOverride" value="Overridden" />
  <add key="KeyToBeAdded" value="EntirelyNew" />
</appSettings>

For your normal appSettings the norm would be to use this: 对于正常的appSettings,规范是使用这个:

<appSettings file="appSettings.config" />

For other sections you can use 对于其他部分,您可以使用

<mySection configSource="mySection.config" />

You have to have a file per section, and the configSource will only work with relative paths. 每个部分必须有一个文件,而configSource只能用于相对路径。

For your particular case, I would recommend a folder for each setup/configuration. 对于您的特定情况,我建议为每个设置/配置建立一个文件夹。

<mySection1 configSource="setup1\mySection1.config" />
<mySection2 configSource="setup1\mySection2.config" />

Then you can manually do a text 'find and replace' on the folder name to switch between setups, or run a batch file or build step that switches the folder on the file system (ie copy the setup1 folder to a currentSetup folder) 然后,您可以在文件夹名称上手动执行文本“查找和替换”以在设置之间切换,或者运行批处理文件或构建步骤以切换文件系统上的文件夹(即将setup1文件夹复制到currentSetup文件夹)

Note that you can't use a configSource on system.serviceModel, but you can for its subsections 请注意,您不能在system.serviceModel上使用configSource,但可以使用其子部分

One approach is to use build events. 一种方法是使用构建事件。 Create two application settings files each named according to the type of deployment. 创建两个应用程序设置文件,每个文件根据部署类型命名。 Create a new build configuration for each type of deployment. 为每种类型的部署创建新的构建配置。

Have a pre-build event that determines the type of build configuration that is being executed and then create the "app.config" file by copying the specialized config file and naming it "app.config". 有一个预构建事件,它确定正在执行的构建配置的类型,然后通过复制专用配置文件并将其命名为“app.config”来创建“app.config”文件。

Add a new setting - AppType = Server/Standalone - duplicate each setting within App.Config that changes based on Server/Standalone so that there is just 1 app.config regardless of Server/Standalone. 添加新设置 - AppType = Server / Standalone - 复制App.Config中基于Server / Standalone更改的每个设置,这样无论Server / Standalone如何,只有1个app.config。 Move the conditional choices into your App. 将条件选项移动到您的应用程序中。

if(AppType == ApplicationType.Server)
{
   Setup(NHibernate);
   Setup(Authentication Implementation);
}
else
{
   Setup(Standalone app stuff);
}

The app.config or web.config file can reference an external config file. app.config或web.config文件可以引用外部配置文件。

<configuration>
  <appSettings file="external.config">
  </appSettings>
</configuration>

MSDN appSettings reference MSDN appSettings参考

System.Configuration supports include files with configsource attribute. System.Configuration支持包含configsource属性的包含文件。 See here http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx or here http://rizwanshah.blogspot.com/2007/10/use-configsource-attribute-to-manage.html 请参阅http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspxhttp://rizwanshah.blogspot.com/2007/10/use-configsource-attribute-to -manage.html

You can use it like this: 你可以像这样使用它:

<SomeSection configSource="myOtherFile.config" />

We've used to add differing sections to declarative wcf configuration etc/ 我们习惯于在声明性wcf配置中添加不同的部分等/

nAnt可能是另一种解决方案。

Simple answer: Just use a command line argument. 简单回答:只需使用命令行参数即可。 I am currently working on a project which needs to switch between 'local' (for testing / debugging purposes) and 'server' (when I deploy it to a production machine). 我目前正在开发一个项目,需要在“本地”(用于测试/调试目的)和“服务器”(当我将其部署到生产机器时)之间切换。 I just have visual studio pass 'local' as a command line argument when I'm in debug mode, and use the presence of that argument to configure the software at runtime to do what I need it to do. 当我处于调试模式时,我只是将visual studio传递给'local'作为命令行参数,并使用该参数的存在来在运行时配置软件以执行我需要它做的事情。

The absence of any argument causes the software to behave as if it's in production. 没有任何参数会导致软件表现得像是在生产中。 When the software is ready for release, I'll just remove the argument logic entirely. 当软件准备发布时,我将完全删除参数逻辑。

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

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