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!
This should do the trick :
Source : https://www.codeproject.com/Articles/616065/Why-Where-and-How-of-NET-Configuration-Files
Ishmaeel's answer generally does work, however I found one issue, which is that using
OpenMappedMachineConfiguration
seems to lose your inherited section groups from machine.config. This means that you can access your own custom sections (which is all the OP wanted), but not the normal system sections. For example, this code will not work:Basically, if you put a watch on the
configuration.SectionGroups
, you'll see that system.net is not registered as a SectionGroup, so it's pretty much inaccessible via the normal channels.There are two ways I found to work around this. The first, which I don't like, is to re-implement the system section groups by copying them from machine.config into your own web.config e.g.
I'm not sure the web application itself will run correctly after that, but you can access the sectionGroups correctly.
The second solution it is instead to open your web.config as an EXE configuration, which is probably closer to its intended function anyway:
I daresay none of the answers provided here, neither mine or Ishmaeel's, are quite using these functions how the .NET designers intended. But, this seems to work for me.