How to access the System.ServiceModel app.config configuration section in code
The app I'm currently working on is a graphical client which connects to a server for its data. This connection is made using WCF and configured within my app.config file like this:
I now need to allow the client to connect to different servers at the request of the user, so I can add these to my app.config as additional endpoint elements, like this:
The problem is, how can I access these configuration elements in code to populate a pull-down or something to let the user pick? The usual ConfigurationManager I'd use to access stuff from my app.config doesn't provide any of the ServiceModel elements so instead I had to first get the Configuration object for the application using ConfigurationManager then get to the service model section using ServiceModelSectionGroup's GetSectionGroup method, like this:
var appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig); var clientEndpoints = serviceModel.Client.Endpoints; foreach (ChannelEndpointElement endpoint in clientEndpoints) { Debug.WriteLine(endpoint.Address); }