Read WCF service endpoint address by name from web.config
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.
Steve
Updated on March 29, 2020Comments
-
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 about 11 yearsMaybe Op does not have a client proxy?
-
Kiquenet over 9 yearsIf you don't want to use ThirdServiceClient class ? Only using ConfigurationManager ?
-
Gul Ershad over 8 yearsGood solution. I just used it. Thanks
-
Aria almost 8 yearsThanks man, less code !!!, to get
EndPoint
by name(not index key) your second snipped code is a good solution. -
interesting-name-here about 7 yearscasting using
as
instead prevents exceptions. nice. -
hal9000 almost 6 yearsthat'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 about 5 yearsOh, 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.