3

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?

Community
  • 1
  • 1
Jeff Loughlin
  • 4,134
  • 2
  • 30
  • 47

1 Answers1

1

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

T.S.
  • 18,195
  • 11
  • 58
  • 78
  • This got me pointed in the right direction. But I had to use OpenMappedWebConfiguration() instead, and map the virtual directory to a physical path first...not sure why I had to do that, but I couldn't seem to get it working any other way. Thanks for the help. – Jeff Loughlin Aug 12 '15 at 12:48
  • This is precisely what I intended - point you toward right configuration manager. Yes, I missed the point that in ASP.NET you need to map things because you need to establish relation of physical to virtual. Here is what I read. **OpenMappedWebConfiguration:** *Opens the Web-application configuration file as a Configuration object `using the specified file mapping` to allow read or write operations.* **OpenWebConfiguration:** *Opens the Web-application configuration file as a Configuration object `using the specified virtual path` to allow read or write operations.* – T.S. Aug 12 '15 at 14:19