C# 从 web.config 按名称读取 WCF 服务端点地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16572890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:22:30  来源:igfitidea点击:

Read WCF service endpoint address by name from web.config

c#wcfweb-config

提问by Steve

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

在这里,我试图从 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.configfile

这是我的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>

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

这会起作用clientSection.Endpoints[0];,但我正在寻找一种按名称检索的方法。

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

即类似的东西clientSection.Endpoints["SecService"],但它不起作用。

采纳答案by McGarnagle

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

回答by marc_s

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.

这样做会自动从配置文件中读取正确的信息。

回答by Wayne Ellery

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

这就是我使用 Linq 和 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:

然后获取等于 endpointName 的端点:

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

Then get the url from the endpoint:

然后从端点获取 url:

var endpointUrl = qasEndpoint?.Address.AbsoluteUri;

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

您还可以使用以下命令从端点接口获取端点名称:

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