Ste*_*eve 18 c# wcf 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();
Run Code Online (Sandbox Code Playgroud)
有没有办法可以根据名称读取终点地址?
这是我的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>
Run Code Online (Sandbox Code Playgroud)
这将工作clientSection.Endpoints[0];,但我正在寻找一种方法来检索名称.
就像这样clientSection.Endpoints["SecService"],但它不起作用.
Way*_*ery 18
这就是我使用Linq和C#6的方法.
首先得到客户端部分:
var client = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
Run Code Online (Sandbox Code Playgroud)
然后获得等于endpointName的端点:
var qasEndpoint = client.Endpoints.Cast<ChannelEndpointElement>()
.SingleOrDefault(endpoint => endpoint.Name == endpointName);
Run Code Online (Sandbox Code Playgroud)
然后从端点获取url:
var endpointUrl = qasEndpoint?.Address.AbsoluteUri;
Run Code Online (Sandbox Code Playgroud)
您还可以使用以下命令从端点接口获取端点名称:
var endpointName = typeof (EndpointInterface).ToString();
Run Code Online (Sandbox Code Playgroud)
McG*_*gle 16
我想你必须实际遍历端点:
string address;
for (int i = 0; i < clientSection.Endpoints.Count; i++)
{
if (clientSection.Endpoints[i].Name == "SecService")
address = clientSection.Endpoints[i].Address.ToString();
}
Run Code Online (Sandbox Code Playgroud)
嗯,每个客户端端点都有一个名称 -只需使用该名称实例化您的客户端代理即可:
ThirdServiceClient client = new ThirdServiceClient("ThirdService");
Run Code Online (Sandbox Code Playgroud)
这样做将自动从配置文件中读取正确的信息。
| 归档时间: |
|
| 查看次数: |
25907 次 |
| 最近记录: |