问题描述
我有一个.NET类库,它提供了一套辅助函数所使用的多种Web服务。此类库必须存储单个设定,具体地,一个连接字符串,其不必由Web看出服务本身,因为它们都必须查询相同datbase
I have a .NET class library that provides a set of helper functions that are used by several Web Services. This class library must store a single setting, specifically, a connection string, which need not be seen by the Web Services themselves, since they all must query the same datbase.
不幸的是,.NET提供了没有办法轻松地阅读一个DLL的的app.config
文件。唯一的易解决方案是将存储在连接字符串中的每一个的Web服务配置文件,这完全是胡说八道。
Unfortunately, .NET provides no means to easily read a DLL's app.config
file. The only "easy" solution would be to store the connection string in every single Web Service configuration file, which is completely bollocks.
通常情况下,我在乎code优雅,但是这一次我的真正的需要一个解决方案,哪怕是一个黑客。有没有什么办法让一个.NET类库都有自己的配置?
Normally, I care about code elegance, but this time I really need a solution, even if it is a hack. Is there any way to make a .NET class library have its own configuration?
编辑:从技术上讲,我可以合并所有这些Web服务到一个单一的Web服务。但是,出于商业原因(每个Web服务将被单独出售),我不能这样做。
Technically, I could merge all those Web Services into a single Web Service. But, for business reasons (each Web Service will be sold separately), I cannot do that.
推荐答案
我想你要找的:
ConfigurationManager.OpenExeConfiguration(string exePath)
或
ConfigurationManager.OpenMappedExeConfiguration(
new ExeConfigurationFileMap() {
ExeConfigFilename = path + "app.config"
}, ConfigurationUserLevel.None);
它返回一个 配置
对象。 在ConfigurationManager中 MSDN文档
尝试这个问题了解如何获得该DLL的路径。
Try this question for how to get the DLL path.
这篇关于我怎样才能让一个.NET类库读取自己的配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!