Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read the "System.ServiceModel" configuration section group from a class library?

Tags:

c#

wcf

wif

Is there any way to access the "System.ServiceModel" client configuaration i.e. app.config in a class based (dll) project?


like image 533
Lets Do Green Avatar asked Aug 16 '16 10:08

Lets Do Green


1 Answers

ConfigurationManager.GetSection(string) lets you open a section from the executing application's app.config or web.config. but system.ServiceModel isn't a section, it's a section group. ConfigurationManager doesn't provide a way to get a section group.

There are ways to get to a Configuration without ConfigurationManager, but it's a little messy because you have to distinguish between an app.config and web.config.

But if you can skip past system.ServiceModel to the actual configuration group that you want then it's really easy because you can use ConfigurationManager. For example,

var section = ConfigurationManager.GetSection("system.serviceModel/client");

Or you can make it strongly typed:

var section = (ClientSection)ConfigurationManager.GetSection("system.serviceModel/client");

or

var behaviorSection = 
    (BehaviorsSection)ConfigurationManager.GetSection("system.serviceModel/behaviors");
like image 167
Scott Hannen Avatar answered Nov 04 '22 07:11

Scott Hannen