Thursday, November 15, 2007

Programmatically enumerate WCF endpoints defined in app.config

For various reasons you might want to enumerate all client endpoints defined in the application configuration file.

For future reference, this is one way to do it:

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

..obviously this is not production quality code.

11 comments:

Anonymous said...

This was exactly what I needed, thanks!

Anonymous said...

can i change the address and have it persist back to the app.config through the configuration manager interface? (if so, how?)

Anonymous said...

Mister, I need modify app.config of my App that contains configuration about WCF (servicemodel)
I need change endpoints for client

Any suggestions or sample code about it ??

thanks in advanced.

If is possible, send me email enrique.prados@a-e.es, for notify answer. Thanks.

fraxedas said...

Thanks, you have saved me some ours of reading. It was exactly what I was looking for.

Richard C said...

Thanks so much for this post! been looking for a easy way to quickly access the endpoints for my server client program! Thank you!

Anonymous said...

thanks bud, worked for me...

Brian T. said...

Nice solution but whn running in partial trust in generates the error "Request for ConfigurationPermission failed while attempting to access configuration section 'system.serviceModel/client'. To allow all callers to access the data for this section, set section attribute 'requirePermission' equal 'false' in the configuration file where this section is declared."

Anyone know the exact syntax for the change to the config file?

I am assuming its something like:
<configSections>
<section name="client" type="System.ServiceModel.Configuration.ClientSection, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</configSections>
But this can't be quite right as I still get the error.

/SiD said...

@Brian T: That seems correct but you'll likely have to do it in machine.config which is a no go if you are running in partial trust.

Anonymous said...

This code works the same and doesn't have that nasty Properties[""] lookup:


var clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
var endpointCollection = clientSection.Endpoints;
var endpointNames = (from ChannelEndpointElement item in endpointCollection select item.Name).ToList();

AryaSomayajula said...

Please send me the code in c# console application.Its very urgent.

Artiom said...

Thx, exactly what I needed