简体   繁体   English

从.net中的类库项目引用设置的最佳实践

[英]Best practice for referencing settings from a class library project in .net

I have multiple projects within my solution. 我的解决方案中有多个项目。 I have a project that is dedicated to holding references a number of WCF Service References ( it holds references to, not implementatins of WCF services ), lets call this project 'GlobalWebServices'. 我有一个专门用于保存许多WCF服务引用的引用的项目(它保存对WCF服务的引用,而不是对WCF服务的实现),我们将此项目称为“ GlobalWebServices”。 I then have many executable applications which reference this GlobalWebServices project. 然后,我有许多引用此GlobalWebServices项目的可执行应用程序。 When I execute these applications, and try to call the references web services, they naturally lose the configuration details for the ServiceReferences that I am wanting to call. 当我执行这些应用程序并尝试调用引用Web服务时,它们自然会丢失我要调用的ServiceReferences的配置详细信息。 The configuration settings belong to the Class Library project - 'GlobalWebServices'. 配置设置属于类库项目-“ GlobalWebServices”。

What is the best way to store these ServiceReference binding details? 存储这些ServiceReference绑定详细信息的最佳方法是什么? I don't want to copy each one into the executing application's .config file because it feels untidy, and I am replicating information. 我不想将每个副本都复制到正在执行的应用程序的.config文件中,因为它感觉很不整洁,并且我正在复制信息。 I don't want to hard code the ServiceReference bindings in the 'GlobalWebServices' application because I have already generated binding connection information for each of the services ( well... visual studio has kindly done it for me, but still ). 我不想在“ GlobalWebServices”应用程序中对ServiceReference绑定进行硬编码,因为我已经为每个服务生成了绑定连接信息(好吧……visual studio替我做了,但是仍然可以)。 Ideally : I would like the GlobalWebServices library to automatically include binding information ( currently stored in it's app.config ) into the class library when it is built. 理想情况下:构建全局库时,我希望GlobalWebServices库自动将绑定信息(当前存储在app.config中)包含到类库中。

What is the best practice solution for this? 最佳实践解决方案是什么?

You can separate out different parts of your web.config using configSection . 您可以使用configSection分离出web.config的不同部分。

There may be a better answer, but my first thought would be to have the config file in your Class Library project. 可能会有更好的答案,但是我的第一个想法是将配置文件包含在类库项目中。 Set the properties of the config file to "Content" and "Copy if Newer". 将配置文件的属性设置为“内容”和“如果更新则复制”。 This will allow you to have it in one location for source control. 这将使您可以将其放在一个位置进行源代码控制。 The downside of this option though is that once it's deployed, it is in several different locations, but that could also be good if the configuration needs to be different for each application that uses the configuration file. 但是,此选项的缺点是,一旦部署,它就位于多个不同的位置,但是如果每个使用该配置文件的应用程序的配置都需要不同,这也可能很好。

I know it's possible to configure services programatically and then write out the configuration to a file. 我知道可以以编程方式配置服务,然后将配置写出到文件中。 I would like to think that would mean you could go the other way, and have a separate XML file, and load the bindings and such from there without it being the app.config itself. 我想这意味着您可以采用其他方法,并拥有一个单独的XML文件,然后从那里加载绑定等,而无需将其作为app.config本身。

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

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