<p>Using C# .NET 3.5 and WCF, I'm trying to write out some of the WCF configuration in a client application (the name of the server the client is connecting to).</p>

<p>The obvious way is to use <code>ConfigurationManager</code> to load the configuration section and write out the data I need.</p>

<pre><code>var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel");
</code></pre>

<p>Appears to always return null.</p>

<pre><code>var serviceModelSection = ConfigurationManager.GetSection("appSettings");
</code></pre>

<p>Works perfectly.</p>

<p>The configuration section is present in the App.config but for some reason <code>ConfigurationManager</code> refuses to load the <code>system.ServiceModel</code> section.</p>

<p>I want to avoid manually loading the xxx.exe.config file and using XPath but if I have to resort to that I will.  Just seems like a bit of a hack.</p>

<p>Any suggestions?</p>

[{'answer_id': 19594, 'author': 'DavidWhitney', 'author_id': 1297, 'author_profile': 'https://Stackoverflow.com/users/1297', 'pm_score': 6, 'selected': False, 'text': '<p><a href="http://mostlytech.blogspot.com/2007/11/programmatically-enumerate-wcf.html" rel="noreferrer">http://mostlytech.blogspot.com/2007/11/programmatically-enumerate-wcf.html</a></p>\n\n<pre><code>// Automagically find all client endpoints defined in app.config\nClientSection clientSection = \n    ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;\n\nChannelEndpointElementCollection endpointCollection =\n    clientSection.ElementInformation.Properties[string.Empty].Value as     ChannelEndpointElementCollection;\nList&lt;string&gt; endpointNames = new List&lt;string&gt;();\nforeach (ChannelEndpointElement endpointElement in endpointCollection)\n{\n    endpointNames.Add(endpointElement.Name);\n}\n// use endpointNames somehow ...\n</code></pre>\n\n<p>Appears to work well.</p>\n'}, {'answer_id': 19596, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 7, 'selected': True, 'text': '<p>The <a href="http://msdn.microsoft.com/en-us/library/ms731354%28v=vs.90%29.aspx" rel="noreferrer"><code>&lt;system.serviceModel&gt;</code></a> element is for a configuration section <strong>group</strong>, not a section. You\'ll need to use <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.configuration.servicemodelsectiongroup.getsectiongroup%28v=vs.90%29.aspx" rel="noreferrer"><code>System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()</code></a> to get the whole group.</p>\n'}, {'answer_id': 19602, 'author': 'DavidWhitney', 'author_id': 1297, 'author_profile': 'https://Stackoverflow.com/users/1297', 'pm_score': 4, 'selected': False, 'text': '<p>This is what I was looking for thanks to @marxidad for the pointer.</p>\n\n<pre><code>    public static string GetServerName()\n    {\n        string serverName = "Unknown";\n\n        Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);\n        ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);\n        BindingsSection bindings = serviceModel.Bindings;\n\n        ChannelEndpointElementCollection endpoints = serviceModel.Client.Endpoints;\n\n        for(int i=0; i&lt;endpoints.Count; i++)\n        {\n            ChannelEndpointElement endpointElement = endpoints[i];\n            if (endpointElement.Contract == "MyContractName")\n            {\n                serverName = endpointElement.Address.Host;\n            }\n        }\n\n        return serverName;\n    }\n</code></pre>\n'}, {'answer_id': 5986619, 'author': 'midspace', 'author_id': 294393, 'author_profile': 'https://Stackoverflow.com/users/294393', 'pm_score': 3, 'selected': False, 'text': '<p>GetSectionGroup() does not support no parameters (under framework 3.5).</p>\n\n<p>Instead use:</p>\n\n<pre><code>Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);\nServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);\n</code></pre>\n'}, {'answer_id': 18844990, 'author': 'Robin G Brown', 'author_id': 2771792, 'author_profile': 'https://Stackoverflow.com/users/2771792', 'pm_score': 3, 'selected': False, 'text': '<p>Thanks to the other posters this is the function I developed to get the URI of a named endpoint. It also creates a listing of the endpoints in use and which actual config file was being used when debugging:</p>\n\n<pre><code>Private Function GetEndpointAddress(name As String) As String\n    Debug.Print("--- GetEndpointAddress ---")\n    Dim address As String = "Unknown"\n    Dim appConfig As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)\n    Debug.Print("app.config: " &amp; appConfig.FilePath)\n    Dim serviceModel As ServiceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup(appConfig)\n    Dim bindings As BindingsSection = serviceModel.Bindings\n    Dim endpoints As ChannelEndpointElementCollection = serviceModel.Client.Endpoints\n    For i As Integer = 0 To endpoints.Count - 1\n        Dim endpoint As ChannelEndpointElement = endpoints(i)\n        Debug.Print("Endpoint: " &amp; endpoint.Name &amp; " - " &amp; endpoint.Address.ToString)\n        If endpoint.Name = name Then\n            address = endpoint.Address.ToString\n        End If\n    Next\n    Debug.Print("--- GetEndpointAddress ---")\n    Return address\nEnd Function\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/19589', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1297/']