简体   繁体   English

引用不同项目时使用app.config?

[英]Using app.config when referencing different projects?

I'm using app.config files for the first time and am unsure how to proceed in a situation with project references and app.config files. 我是第一次使用app.config文件,不确定如何处理项目引用和app.config文件。 Here's a description of my projects I'm working with: 这是我正在处理的项目的描述:

Project1 (process logger) populates log table in database specified in Project1 app.config file. Project1 (过程记录器)将在Project1 app.config文件中指定的数据库中填充日志表。

Project2 (calculator) runs a calculation and populates a table in database specified in Project2 app.config file. Project2 (计算器)运行计算并填充Project2 app.config文件中指定的数据库中的表。 Records log using reference to Project1. 使用对Project1的引用记录日志。

When I reference Project1 in Project2, in my "Release" folder I do not see an app.config file for Project1. 当我在Project2中引用Project1时,在“发布”文件夹中没有看到Project1的app.config文件。 What is the best way to handle this situation? 处理这种情况的最佳方法是什么? Instead of making the database configurable in an app.config file in Project1, should I instead make it a parameter for Project2 to specify (Project2.LogConnectionString is sent as a parameter to functions in Project1)? 而不是使数据库在Project1的app.config文件中可配置,而不是使它成为供Project2指定的参数(将Project2.LogConnectionString作为参数发送给Project1中的函数)? Or is there a way to make the app.config file appear in my "Release" folder? 还是有办法使app.config文件出现在我的“发布”文件夹中?

Any help would be appreciated, I have no idea what is the right way to do this. 任何帮助将不胜感激,我不知道什么是正确的方法来做到这一点。 Thanks! 谢谢!

Configuration files are based on the startup project . 配置文件基于启动项目 Which means that when you instantiate something from proj1 in proj2 (proj1 will be a reference in proj2) you can use the app.config from proj2. 这意味着,当您在proj2中的proj1中实例化某些东西时(proj1将是proj2中的引用),您可以使用proj2中的app.config。

Basically, you can store all config in proj2 and use as need. 基本上,您可以将所有配置存储在proj2中并根据需要使用。

btw, change the proj names ;) 顺便说一句,更改项目名称;)

The best option for you would be to load Project1 confing inside Project2 at run time. 最好的选择是在运行时将Project1 confing加载到Project2中。 What I would do is to place configuration string (or custom configuration section if required) inside Project2 confing. 我要做的是将配置字符串(如果需要,或自定义配置部分)放置在Project2 confing中。 This settings would basicly just tell app where to look Project1 config. 此设置基本上只会告诉应用程序在哪里查看Project1配置。

Once you have confing path you can load it like this: 一旦有了会话路径,就可以像这样加载它:

    System.Configuration.ExeConfigurationFileMap configFile = new System.Configuration.ExeConfigurationFileMap();
    configFile.ExeConfigFilename = "my_file.config";
    config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFile, System.Configuration.ConfigurationUserLevel.None);
    settings = config.AppSettings.Settings;

In this case I am accessing AppSettings from Project1. 在这种情况下,我正在从Project1访问AppSettings。

And about custom configuration that I have mentioned, please see one of my posts, this one Custom configuration section will do the job if you will require more complex settings. 关于我提到的自定义配置,请参阅我的一篇文章,如果您需要更复杂的设置,则此“ 自定义配置”部分将可以完成此工作。

If you use default Properties.Settings (difference between app.config and settings explained here ) and you have Project1 referenced in Project2, then you may do it like this: 如果您使用默认的Properties.Settings(app.config和设置之间的差异在此处说明),并且在Project2中引用了Project1,则可以这样进行操作:

// in Project2: 
    string connectionstring = NamespaceOfProject1.Properties.Settings.Default.Project1ConnString;

A .NET application has one configuration file. .NET应用程序具有一个配置文件。 It usually has the name of the executable. 它通常具有可执行文件的名称。 .NET doesn't support a config file per assembly. .NET不支持每个程序集的配置文件。

You should be passing the connection string from the executable to the logger project. 您应该将连接字符串从可执行文件传递到logger项目。

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

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