How to avoid duplicate settings when using 2 projects and 2 testing projects in Visual Studio?

被刻印的时光 ゝ 提交于 2019-12-11 16:02:57

问题


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?

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.


回答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.




回答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:

  1. Right-click the other project.
  2. Select Add -> Existing Item and navigate to thh app.config.
  3. Don't click Add! Click the little arrow on the right side of the button, and select Add as Link.

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.

<appSettings file="commonSetting.config">

</appSettings>

Maybe something similar can be used in your case?



来源:https://stackoverflow.com/questions/484266/how-to-avoid-duplicate-settings-when-using-2-projects-and-2-testing-projects-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!