Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read WCF service endpoint address by name from web.config

Tags:

c#

wcf

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.

like image 914
Steve Avatar asked May 15 '13 18:05

Steve


People also ask

What is endpoint configuration name in WCF?

The endpoint with the name "http" is used as the default endpoint for the service application.

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 EndpointAddress?

The endpoint address is represented by the EndpointAddress class, which contains a Uniform Resource Identifier (URI) that represents the address of the service, an Identity, which represents the security identity of the service, and a collection of optional Headers.


3 Answers

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();
like image 196
Wayne Ellery Avatar answered Oct 16 '22 07:10

Wayne Ellery


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();
}
like image 17
McGarnagle Avatar answered Oct 16 '22 07:10

McGarnagle


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.

like image 5
marc_s Avatar answered Oct 16 '22 08:10

marc_s