在Visual Studio中使用2个项目和2个测试项目时,如何避免重复设置?

时间:2022-05-09 08:35:48

Following my previous question, now I have 2 projects: one Console Project and a Library Project. They also have their respective unit test projects. When I run a test for the console project that uses a method from the library project and the library project needs an app.config setting, that setting must be in the console test project's app.config. What I have to do to make it read the setting from the library project app.config, so then I do not have to duplicate the setting across multiple app.config?

根据我之前的问题,现在我有2个项目:一个控制台项目和一个图书馆项目。他们还有各自的单元测试项目。当我为使用库项目中的方法的控制台项目运行测试并且库项目需要app.config设置时,该设置必须位于控制台测试项目的app.config中。我需要做些什么才能让它从库项目app.config中读取设置,那么我不必在多个app.config中复制设置?

Update I don't want to use the same App.Config for both projects. What I am having to do now, but don't want to do anymore, is copying all the library app.config settings into the console project settings.

更新我不想为两个项目使用相同的App.Config。我现在必须做的,但不想再做了,将所有库app.config设置复制到控制台项目设置中。

2 个解决方案

#1


If the library project is a class library, then it needs a context to execute in. VS will not read the app.config file for a class library, it only reads the app.config file from the executing context, from what I can tell. The setting has to be in the console app.config because that is the context for execution. If you are running unit tests and they are the executing context then they will need their own app.config as well.

如果库项目是一个类库,那么它需要一个上下文来执行.VS不会读取类库的app.config文件,它只从执行的上下文中读取app.config文件,从我所知道的。该设置必须位于控制台app.config中,因为这是执行的上下文。如果您正在运行单元测试并且它们是执行上下文,那么它们也需要自己的app.config。

#2


One option I have used is to share the config file via a soft link. First create the app.config file in one project, then add it to the other:

我使用的一个选项是通过软链接共享配置文件。首先在一个项目中创建app.config文件,然后将其添加到另一个项目中:

  1. Right-click the other project.
  2. 右键单击其他项目。

  3. Select Add -> Existing Item and navigate to thh app.config.
  4. 选择Add - > Existing Item并导航到app.config。

  5. Don't click Add! Click the little arrow on the right side of the button, and select Add as Link.
  6. 不要单击添加!单击按钮右侧的小箭头,然后选择添加为链接。

This of course assumes you can use the same file for both projects.

当然,这假设您可以为两个项目使用相同的文件。

UPDATE: I do not know which part of the app.config you need to share. But for the appSettings section, you can include the contents of another file via the "file" attribute.

更新:我不知道你需要分享app.config的哪一部分。但是对于appSettings部分,您可以通过“file”属性包含另一个文件的内容。

<appSettings file="commonSetting.config">

</appSettings>

Maybe something similar can be used in your case?

也许在你的情况下可以使用类似的东西?

#1


If the library project is a class library, then it needs a context to execute in. VS will not read the app.config file for a class library, it only reads the app.config file from the executing context, from what I can tell. The setting has to be in the console app.config because that is the context for execution. If you are running unit tests and they are the executing context then they will need their own app.config as well.

如果库项目是一个类库,那么它需要一个上下文来执行.VS不会读取类库的app.config文件,它只从执行的上下文中读取app.config文件,从我所知道的。该设置必须位于控制台app.config中,因为这是执行的上下文。如果您正在运行单元测试并且它们是执行上下文,那么它们也需要自己的app.config。

#2


One option I have used is to share the config file via a soft link. First create the app.config file in one project, then add it to the other:

我使用的一个选项是通过软链接共享配置文件。首先在一个项目中创建app.config文件,然后将其添加到另一个项目中:

  1. Right-click the other project.
  2. 右键单击其他项目。

  3. Select Add -> Existing Item and navigate to thh app.config.
  4. 选择Add - > Existing Item并导航到app.config。

  5. Don't click Add! Click the little arrow on the right side of the button, and select Add as Link.
  6. 不要单击添加!单击按钮右侧的小箭头,然后选择添加为链接。

This of course assumes you can use the same file for both projects.

当然,这假设您可以为两个项目使用相同的文件。

UPDATE: I do not know which part of the app.config you need to share. But for the appSettings section, you can include the contents of another file via the "file" attribute.

更新:我不知道你需要分享app.config的哪一部分。但是对于appSettings部分,您可以通过“file”属性包含另一个文件的内容。

<appSettings file="commonSetting.config">

</appSettings>

Maybe something similar can be used in your case?

也许在你的情况下可以使用类似的东西?