How can I make a .NET class library read its own c

2019-01-22 03:45发布

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.

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.

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?


EDIT: 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.

7条回答
Bombasti
2楼-- · 2019-01-22 03:57

Would it be an option to reference the library's configuration file from each web service's configuration? There is an XML include-like mechanism in .NET:

Use XML includes or config references in app.config to include other config files' settings

http://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx

While you'd still need to edit each web.config, the actual content is maintained in a single place.

查看更多
乱世女痞
3楼-- · 2019-01-22 03:59

The ConfigurationManager.OpenExeConfiguration didn't work for me.

But, Properties.Settings.Default.<SettingName> worked well.

查看更多
Fickle 薄情
4楼-- · 2019-01-22 04:03

You can read the configuration settings of the class library from the hosting application's web.config or app.config.

If the class library is referenced in a console application, put the settings, the class library needs, in the app.config of the console application (say under appSettings) and read it from the class library using ConfigurationManager under System.Configuration.

If the class library is referenced in a web application, put the settings the class library needs in the web.config of the web application (say under appSettings) and read it from the class library using ConfigurationManager under System.Configuration.

查看更多
Luminary・发光体
5楼-- · 2019-01-22 04:07

I think you're looking for:

ConfigurationManager.OpenExeConfiguration(string exePath)

or

ConfigurationManager.OpenMappedExeConfiguration(
    new ExeConfigurationFileMap() { 
        ExeConfigFilename = path + "app.config" 
    }, ConfigurationUserLevel.None);

Which returns a Configuration object. MSDN doc on ConfigurationManager

Try this question for how to get the DLL path.

查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-22 04:08

You can use the opensource Config.Net library which supports multiple sources of data, including your one https://github.com/aloneguid/config

查看更多
仙女界的扛把子
7楼-- · 2019-01-22 04:17

This kind of configuration issue is solved quite nicely using Enterprise Library "Shared Configuration Sources" and "Differential Configurations" (for easily switching between multiple environments, etc.).

To learn about Advanced Configuration Scenarios, try starting here:

http://msdn.microsoft.com/en-us/library/ff664552(v=pandp.50).aspx

And to integrate the Enterprise Library Configuration Tool (GUI) in Visual Studio, try here:

http://visualstudiogallery.msdn.microsoft.com/029292f0-6e66-424f-8381-3454c8222f9a

The learning curve may seem a bit overwhelming at first, but it is well worth the effort, especially if you are dealing with complex enterprise ecosystems. The integrated tool actually makes it pretty easy to set up a very sophisticated configuration system, and manage it as your requirements change.

BTW: Once you get the hang of it, you'll probably end up wanting to use it for a lot more than just your Connection Strings!

查看更多
登录 后发表回答