Download System.ServiceModel with ConfigurationManager

Using C # .NET 3.5 and WCF, I am trying to write out part of the WCF configuration in a client application (the name of the server to which the client connects).

The obvious way is to use the ConfigurationManager to load the configuration section and write the data I need.

 var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel"); 

Appears to always return null.

 var serviceModelSection = ConfigurationManager.GetSection("appSettings"); 

Works great.

The configuration section is present in App.config, but for some reason the ConfigurationManager refuses to load the system.ServiceModel section.

I do not want to manually download the xxx.exe.config file and use XPath, but if I have to resort to what I will do. It just seems like it's a hack.

Any suggestions?

+55
c # xml wcf configurationmanager
Aug 21 '08 at 10:27
source share
5 answers

The <system.serviceModel> element refers to the group configuration section, not the section. You will need to use System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup() to get the whole group.

+45
Aug 21 '08 at 10:38
source share

http://mostlytech.blogspot.com/2007/11/programmatically-enumerate-wcf.html

 // Automagically find all client endpoints defined in app.config ClientSection clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection; ChannelEndpointElementCollection endpointCollection = clientSection.ElementInformation.Properties[string.Empty].Value as ChannelEndpointElementCollection; List<string> endpointNames = new List<string>(); foreach (ChannelEndpointElement endpointElement in endpointCollection) { endpointNames.Add(endpointElement.Name); } // use endpointNames somehow ... 

Appears to work well.

+55
Aug 21 '08 at 10:35
source share

This is what I was looking for thanks to @marxidad for a pointer.

  public static string GetServerName() { string serverName = "Unknown"; Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig); BindingsSection bindings = serviceModel.Bindings; ChannelEndpointElementCollection endpoints = serviceModel.Client.Endpoints; for(int i=0; i<endpoints.Count; i++) { ChannelEndpointElement endpointElement = endpoints[i]; if (endpointElement.Contract == "MyContractName") { serverName = endpointElement.Address.Host; } } return serverName; } 
+14
Aug 21 '08 at 10:51
source share

GetSectionGroup () does not support any parameters (within 3.5).

Use instead:

 Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config); 
+8
May 13 '11 at 2:19
source share

Thanks to other posters, this is a function that I developed to get a named endpoint URI. It also creates a list of used endpoints and what actual configuration file is used for debugging:

 Private Function GetEndpointAddress(name As String) As String Debug.Print("--- GetEndpointAddress ---") Dim address As String = "Unknown" Dim appConfig As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) Debug.Print("app.config: " & appConfig.FilePath) Dim serviceModel As ServiceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup(appConfig) Dim bindings As BindingsSection = serviceModel.Bindings Dim endpoints As ChannelEndpointElementCollection = serviceModel.Client.Endpoints For i As Integer = 0 To endpoints.Count - 1 Dim endpoint As ChannelEndpointElement = endpoints(i) Debug.Print("Endpoint: " & endpoint.Name & " - " & endpoint.Address.ToString) If endpoint.Name = name Then address = endpoint.Address.ToString End If Next Debug.Print("--- GetEndpointAddress ---") Return address End Function 
+7
Sep 17 '13 at 8:34
source share



All Articles