Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading System.ServiceModel configuration section using ConfigurationManager

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).

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

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

Appears to always return null.

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

Works perfectly.

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

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.

Any suggestions?

like image 801
DavidWhitney Avatar asked Aug 21 '08 10:08

DavidWhitney


People also ask

What is ServiceModel in web config?

system. serviceModel is a root element of all WCF configuration elements. The configuration information for a service is contained within a system.

What is System ServiceModel used for?

ClientBase<TChannel> Class (System.ServiceModel) Provides the base implementation used to create Windows Communication Foundation (WCF) client objects that can call services.

What is the use of ConfigurationManager in C#?

The ConfigurationManager class enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated. For web applications, use the WebConfigurationManager class.


1 Answers

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.

like image 96
DavidWhitney Avatar answered Oct 04 '22 17:10

DavidWhitney