从web.config中按名称读取WCF服务端点地址 [英] Read WCF service endpoint address by name from web.config

查看:41
本文介绍了从web.config中按名称读取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?

这是我的 web.config 文件

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

这将有效 clientSection.Endpoints [0]; ,但我正在寻找一种按名称检索的方法.

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

即类似于 clientSection.Endpoints ["SecService"] ,但无法正常工作.

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

推荐答案

我想您实际上必须遍历端点:

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

这篇关于从web.config中按名称读取WCF服务端点地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆