My ASP.NET web service has the following custom configSections defined in its web.config:
<configSections>
<section name="config1" type="MyWebService.Configuration.MyWebServiceSection, App_Code"/>
<section name="config2" type="MyWebService.Configuration.MyWebServiceSection, App_Code"/>
</configSections>
From within a WebMethod, I need to get the list of section names defined there, which in this example would be "config1" and "config2". From this answer, I learned that a standalone application can get what I'm looking for by using something like:
Configuration config = ConfigurationManager.OpenExeConfiguration(pathToExecutable);
foreach (ConfigurationSection cs in config.Sections)
{
sectionNames.Add(cs.SectionInformation.SectionName);
}
but I need to do this from inside a web service, so I don't have a path to an executable. There is a suggestion in the comments on that answer to use ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); instead of the path to the executable, but that didn't work for me either (System.Argument exception: "exePath must be specified when not running inside a stand alone exe").
Is this the right approach, and if so, what should I pass to OpenExeConfiguration when I'm running inside a web service?
For asp.net use this. You need to use WebConfigurationManager for web applications
var conf = WebConfigurationManager.OpenWebConfiguration("<path>")
foreach(var sec in conf.Sections)
{
. . . .
}
https://msdn.microsoft.com/en-us/library/system.configuration.configuration.sections%28v=vs.110%29.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With