Read WCF service endpoint address by name from web.config

30,013

Solution 1

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();
}

Solution 2

This is how I did it using Linq and C# 6.

First get the client section:

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

Then get the endpoint that's equal to endpointName:

var qasEndpoint = client.Endpoints.Cast<ChannelEndpointElement>()
    .SingleOrDefault(endpoint => endpoint.Name == endpointName);

Then get the url from the endpoint:

var endpointUrl = qasEndpoint?.Address.AbsoluteUri;

You can also get the endpoint name from the endpoint interface by using:

var endpointName = typeof (EndpointInterface).ToString();

Solution 3

Well, each client-side endpoint has a name - just instantiate your client proxy using that name:

ThirdServiceClient client = new ThirdServiceClient("ThirdService");

Doing this will read the right information from the config file automatically.

Share:
30,013
Steve
Author by

Steve

Updated on March 29, 2020

Comments

  • Steve
    Steve about 4 years

    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.

  • McGarnagle
    McGarnagle about 11 years
    Maybe Op does not have a client proxy?
  • Kiquenet
    Kiquenet over 9 years
    If you don't want to use ThirdServiceClient class ? Only using ConfigurationManager ?
  • Gul Ershad
    Gul Ershad over 8 years
    Good solution. I just used it. Thanks
  • Aria
    Aria almost 8 years
    Thanks man, less code !!!, to get EndPoint by name(not index key) your second snipped code is a good solution.
  • interesting-name-here
    interesting-name-here about 7 years
    casting using as instead prevents exceptions. nice.
  • hal9000
    hal9000 almost 6 years
    that's great but I had to use.ToList() before it would let me use SingleOrDefault client.Endpoints.Cast<ChannelEndpointElement>().ToList(), in case that is anyone elses issue
  • Suncat2000
    Suncat2000 about 5 years
    Oh, that's from a Microsoft example that created the proxy class using the svcutil.exe program against a web service. I wondered what part of the .Net library ThirdServiceClient was in - but it was from a sample program. Gee, @marc_s, I think that's one of the most incomprehensible answers I've ever seen from you. Bad day, huh.