У меня есть app.config файл что в форме:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://something.com"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransfer"
contract="ABC" name="XXX" />
<endpoint address="http://something2.com"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransfer"
contract="ABC2" name="YYY" />
</client>
</system.serviceModel>
</configuration>
Я хочу считать значение в атрибуте "адрес" конечной точки узла, которая имеет имя = "XXX". Покажите мне, как сделать это!
(Продолжите реветь обсуждение с marc_s. Извините, что поместил текст здесь, так как комментарий не позволяет форматировать коды), @marc_s: Я использую ниже кодов для чтения вышеупомянутого файла, но он показывает что clientSection. Конечные точки имеют 0 участников (Count=0). Помогите!
public MainWindow()
{
var exeFile = Environment.GetCommandLineArgs()[0];
var configFile = String.Format("{0}.config", exeFile);
var config = ConfigurationManager.OpenExeConfiguration(configFile);
var wcfSection = ServiceModelSectionGroup.GetSectionGroup(config);
var clientSection = wcfSection.Client;
foreach (ChannelEndpointElement endpointElement in clientSection.Endpoints)
{
if (endpointElement.Name == "XXX")
{
var addr = endpointElement.Address.ToString();
}
}
}
В этом нет необходимости - среда выполнения WCF сделает все это за вас.
Если вам действительно необходимо - по какой-либо причине - вы можете сделать это:
using System.Configuration;
using System.ServiceModel.Configuration;
ClientSection clientSettings = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
string address = null;
foreach(ChannelEndpointElement endpoint in clientSettings.Endpoints)
{
if(endpoint.Name == "XXX")
{
address = endpoint.Address.ToString();
break;
}
}
Вы можете использовать ServiceModelSectionGroup (System.ServiceModel.Configuration) для доступа к конфигурации:
var config = ConfigurationManager.GetSection("system.serviceModel") as ServiceModelSectionGroup;
foreach (ChannelEndpointElement endpoint in config.Client.Endpoints)
{
Uri address = endpoint.Address;
// Do something here
}
Надеюсь, что это поможет.
var config = ConfigurationManager.OpenExeConfiguration("MyApp.exe.config");
var wcfSection = ServiceModelSectionGroup.GetSectionGroup(config);
var clientSection = wcfSection.Client;
foreach(ChannelEndpointElement endpointElement in clientSection.Endpoints) {
if(endpointElement.Name == "XXX") {
return endpointElement.Address;
}
}