解决方案的单个配置文件 [英] Single config file for solution

查看:58
本文介绍了解决方案的单个配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我已经以一种不同的方式在SO上看到了这个问题,但令人惊讶的是,这种形式没有出现:

Now I have seen this question before on SO in a variant ways, but surprisingly not in this form:

我有一个包含多个需要相互通信的Web服务(项目)的解决方案.发布这些Web服务之后,它们可能最终会在具有不同数据库的不同计算机上结束.为了告诉每个Web服务所有其他Web服务在哪里,我想在开发过程中维护一个配置文件.

I have a solution with multiple web services (projects) that need to talk to each other. After publishing each of these web services might end up on a different machine with a different database. To tell each web service where all other web services are, I want to maintain a single config file during development.

我希望在发布配置后在每个已发布的项目中都存在该配置.而且我希望配置文件在发布后是可编辑的,因此我可以迅速迁移某个Web服务,然后只编辑其他Web服务的所有配置文件.

I would like to expect that after publishing the config to be present in each published project. And I would like to expect the config file to be editable after publishing, so I can swiftly migrate a certain web service and then just edit all config files of the other web services.

我不想在数据库中执行此操作,因为配置文件本身也应该保存与数据库的连接设置.

I don't want to do this in the database, for the config file its self should also hold connection settings to the database(s).

我遇到了以下想法/想法/问题:

I came across the following ideas/thoughts/questions:

  • 我有一个名为"common"的dll项目,该项目被其他项目引用.让我们给它一个shared.config并在该项目中构建一个类,该类可以通过执行 System.Configuration.ConfigurationManager.OpenExeConfiguration("shared.config")来读取shared.config.只需确保shared.config将与DLL一起发布即可.
  • I have a dll project called 'common' that is referenced by other projects. Let's give that one a shared.config and build a class in that project that can be used to read out the shared.config by doing System.Configuration.ConfigurationManager.OpenExeConfiguration("shared.config"). Just need to make sure the shared.config will be published along with the DLL.

我更喜欢这种解决方案,因为它还可以让我在每个项目中都保留一个web.config,而该项目只有项目特定的设置.并让shared.config具有共享设置.但是我在SO上读到,这不应被轻描淡写,并且可能会产生一些有害的副作用,例如文件访问问题.尽管我想知道这是否适用于我的情况.另外,我想在这里就如何实际实现这一问题寻求帮助,因为我认为Visual Studio不支持DLL项目的app.config.

I would favor this solution, as it would also let me keep a web.config inside each project having just the project specific settings. And have the shared.config having the shared settings. But I read on SO that this should not be considered lightly and could have some unwanted side-effects, like file-access-issues; though I wonder if this would apply to my case. Also I would like to ask your help here on how to actually realize this as I don't think Visual Studio supports app.config for DLL projects out of the box.

  • 我还考虑过在解决方案项目中创建一个shared.config文件.然后在每个项目中链接该文件.在每个项目的Web.config中,添加:< appSettings configSource ="shared.config"/> 指向该项目中的链接文件.
  • I also thought about creating a shared.config file in the Solution Items. Then linking that file inside each project. And in the Web.config of each projects, add: <appSettings configSource="shared.config" /> pointing to the linked file in that project.

尽管我找不到任何不这样做的原因,但第一个实现失败.看来(至少在开发过程中),c#找不到链接的shared.config文件.我猜链接文件在创建链接文件后不会立即完成,也不会得到维护,但是在我执行发布时,文件只会复制到项目中.因此在开发过程中缺少文件.这是正确的吗?

Though I cannot find any reason why not to do this, first implementation failed. It seems (at least during development), c# cannot find the linked shared.config file. I'm guessing linking files is not done instantly nor maintained after creating the linked file, but the file is only copied to the projects WHEN I do a publish. Thus leaving the file missing during development. Is this correct?

推荐答案

配置文件是特定于应用程序的.这意味着您可以将配置文件添加到类库,但是该文件随后将由引用该库的应用程序(Windows服务,Web服务等)使用.

The config files are app specific. This mean that you can add a config file to a class library but the file will then by used by the app (windows service, webservice and so on) referencing the library.

与外部 configSource 相同,这也是特定于应用程序的,使用它的项目需要包含在其中.因此,如果您的解决方案由2个项目组成,则需要2个配置文件.每个项目一个.

Same thing for external configSource, this are app specific as well and need to be included withing the project using it. So if your solution is composed by 2 projects you then need 2 config files. One for each project.

对于基于Windows的应用程序(服务,winforms),配置文件的预期文件夹为 bin 目录,对于基于Web的项目,此目录将为虚拟目录的根文件夹.

While for a windows based application(services, winforms) the expected folder for config files is the bin directory, for web based projects this will be the directory is the root folder of the virtual directory.

这就是说,使用共享的配置文件看起来是更简单的解决方案(并且您不必从每个项目的类库中复制app.config).步骤如下:

This said, using a shared config file looks the easier solution (and you don't have to copy the app.config from the class library for each project). Here are the steps :

  • 创建解决方案文件夹.
  • 向其中添加配置文件.
  • 将文件添加为每个需要它的项目的参考.右键单击项目,然后 添加现有项目 ->选择文件,然后 添加为链接
  • 通过始终复制设置复制选项(文件属性),以确保始终复制文件.

此时,每次编译解决方案时,都应将配置文件部署到项目目录中.

At this point you should have the config file deployed into your project directory everytime you compile the solution.

  • 我会避免查看Web应用程序中配置文件的bin,约定是该文件应位于根目录中,因此我会避免首选.
  • 链接的文件最终在构建项目后位于bin中.尝试执行相同的步骤来导入文件,但是这次只需添加它(不作为链接),它就会作为内容部署在您网站的根目录中,因此始终可以使用.
  • I'd avoid looking into the bin for config files within a web app, the convention is that file should be in the root, so I would avoid the first option.
  • Linked files end up in the bin after building the project. Try the same steps for importing the file but this time simply add it (not as link) and it will be deployed as content in the root of your site, so it can be always available.

这篇关于解决方案的单个配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆