Search code examples
c#wcfweb-config

Read WCF service endpoint address by name from web.config


Here I am trying to read my service endpoint address by name from web.config

ClientSection clientSection = (ClientSection)ConfigurationManager.GetSection("system.serviceModel/client");
var el = clientSection.Endpoints("SecService"); // I don't want to use index here as more endpoints may get added and its order may change
string addr = el.Address.ToString();

Is there a way I can read end point address based on name?

Here is my web.config file

<system.serviceModel>
 <client>
     <endpoint address="https://....................../FirstService.svc" binding="wsHttpBinding" bindingConfiguration="1ServiceBinding" contract="abc.firstContractName" behaviorConfiguration="FirstServiceBehavior" name="FirstService" />
     <endpoint address="https://....................../SecService.svc" binding="wsHttpBinding" bindingConfiguration="2ServiceBinding" contract="abc.secContractName" behaviorConfiguration="SecServiceBehavior" name="SecService" />
     <endpoint address="https://....................../ThirdService.svc" binding="wsHttpBinding" bindingConfiguration="3ServiceBinding" contract="abc.3rdContractName" behaviorConfiguration="ThirdServiceBehavior" name="ThirdService" />
            </client>
    </system.serviceModel>

This will work clientSection.Endpoints[0];, but I am looking for a way to retrieve by name.

I.e. something like clientSection.Endpoints["SecService"], but it's not working.


Solution

  • I guess you have to actually iterate through the endpoints:

    string address;
    for (int i = 0; i < clientSection.Endpoints.Count; i++)
    {
        if (clientSection.Endpoints[i].Name == "SecService")
            address = clientSection.Endpoints[i].Address.ToString();
    }