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.
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.
The
ConfigurationManager.OpenExeConfiguration
didn't work for me.But,
Properties.Settings.Default.<SettingName>
worked well.You can read the configuration settings of the class library from the hosting application's
web.config
orapp.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 underSystem.Configuration
.I think you're looking for:
or
Which returns a
Configuration
object. MSDN doc on ConfigurationManagerTry this question for how to get the DLL path.
You can use the opensource Config.Net library which supports multiple sources of data, including your one https://github.com/aloneguid/config
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!