![](/img/trans.png)
[英]How to read an endpoint value from an app.config and pass it to my proxy class for the client?
[英]how to read endpoint address value from app.config
我有asp.net web application
並且我的app.config
文件中包含以下內容。
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="Test" />
<binding name="Test1">
<security mode="Test" />
</binding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://test:90001"
binding="basicHttpBinding" bindingConfiguration="Test"
contract="Test" name="HTTP_Port" />
</client>
</system.serviceModel>
我如何get/read endpoint address
值?
我遇到了這個解決方案,但它不適用於我要求exe文件路徑。 不確定哪個exe路徑文件?
var serviceModel = ServiceModelSectionGroup.GetSectionGroup(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None));
var endpoints = serviceModel.Client.Endpoints;
foreach (ChannelEndpointElement e in endpoints)
{
if (e.Name == "HTTP_Port")
Console.WriteLine(e.Address);
}
Console.ReadLine();
要從appconfig中讀取端點,您需要添加System.ServiceModel.Configuration的引用,並使用GetSectionGroup方法返回serviceModel組,並且您可以訪問該部分中的所有端點,如果只需要一個可以使用端點名稱訪問的端點。
同樣在appconfig中,您提供的端口無效,在讀取帶有該端口的url時出現錯誤,我假設您只是添加了該示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.