I'm developing a data access component that will be used in a website that contains a mix of classic ASP and ASP.NET pages, and need a good way to manage its configuration settings.
I'd like to use a custom ConfigurationSection
, and for the ASP.NET pages this works great. But when the component is called via COM interop from a classic ASP page, the component isn't running in the context of an ASP.NET request and therefore has no knowledge of web.config.
Is there a way to tell the ConfigurationManager
to just load the configuration from an arbitrary path (e.g. ..\web.config
if my assembly is in the /bin
folder)? If there is then I'm thinking my component can fall back to that if the default ConfigurationManager.GetSection
returns null
for my custom section.
Any other approaches to this would be welcome!
Use XML processing:
Try this:
Another solution is to override the default environment configuration file path.
I find it the best solution for the of non-trivial-path configuration file load, specifically the best way to attach configuration file to dll.
Example:
More details may be found at this blog.
Additionally, this other answer has an excellent solution, complete with code to refresh the app config and an
IDisposable
object to reset it back to it's original state. With this solution, you can keep the temporary app config scoped:In addition to Ishmaeel's answer, the method
OpenMappedMachineConfiguration()
will always return aConfiguration
object. So to check to see if it loaded you should check theHasFile
property where true means it came from a file.I provided the configuration values to word hosted .nET Compoent as follows.
A .NET Class Library component being called/hosted in MS Word. To provide configuration values to my component, I created winword.exe.config in C:\Program Files\Microsoft Office\OFFICE11 folder. You should be able to read configurations values like You do in Traditional .NET.
For ASP.NET use WebConfigurationManager: