【问题标题】:How to read value of an attribute defined in app.config?如何读取 app.config 中定义的属性的值?
【发布时间】:2011-03-10 02:22:42
【问题描述】:

我有一个 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.Endpoints 有 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();
            }
        }
    }

【问题讨论】:

  • 您不应该使用 *vshost.config" 文件 - 只有在 Visual Studio 中运行程序时才会出现该文件。请改用 MyApp.exe.config !!
  • @marc_s:嗨,我已经更新了我的代码。但是,读取的端点数量仍然为零。
  • 是的,我可以看到——不过,使用我的代码它可以工作。试试吧!不要使用您的 ServiceModelSectionGroup.GetSectionGroup(config); 和以下行,而是使用我的代码 (ConfigurationManager.GetSection(....)) - 适用于我的情况!
  • @marc_s:我试过你的代码,它可以工作!非常感谢!

标签: c# xml configuration wshttpbinding


【解决方案1】:
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;
    }
}

【讨论】:

    【解决方案2】:

    您可以使用 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
        }
    

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      您真的不需要 - 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;
         }
      }
      

      【讨论】:

      • 我们如何定义类 ClientSection 以在您的代码中使用?我不确定如何定义 Endpoints 属性。我们可以在那里使用 ArrayList 吗?
      • @Nam Gi VU:ClientSection 在 System.ServiceModel.Configuration 中定义,它有一个 Endpoints 集合。你没有在这里定义任何东西——你只是在使用现有的框架类。
      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 2015-09-01
      • 2010-11-12
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      相关资源
      最近更新 更多