【问题标题】:Loading System.ServiceModel configuration section using ConfigurationManager使用 ConfigurationManager 加载 System.ServiceModel 配置部分
【发布时间】:2010-09-06 09:11:52
【问题描述】:

使用 C# .NET 3.5 和 WCF,我试图在客户端应用程序中写出一些 WCF 配置(客户端连接到的服务器的名称)。

显而易见的方法是使用ConfigurationManager 来加载配置部分并写出我需要的数据。

var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel");

似乎总是返回 null。

var serviceModelSection = ConfigurationManager.GetSection("appSettings");

完美运行。

配置部分存在于 App.config 中,但由于某种原因 ConfigurationManager 拒绝加载 system.ServiceModel 部分。

我想避免手动加载 xxx.exe.config 文件并使用 XPath,但如果我不得不求助于它,我会的。只是看起来有点像 hack。

有什么建议吗?

【问题讨论】:

    标签: c# .net xml wcf configurationmanager


    【解决方案1】:

    http://mostlytech.blogspot.com/2007/11/programmatically-enumerate-wcf.html

    // Automagically find all client endpoints defined in app.config
    ClientSection clientSection = 
        ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
    
    ChannelEndpointElementCollection endpointCollection =
        clientSection.ElementInformation.Properties[string.Empty].Value as     ChannelEndpointElementCollection;
    List<string> endpointNames = new List<string>();
    foreach (ChannelEndpointElement endpointElement in endpointCollection)
    {
        endpointNames.Add(endpointElement.Name);
    }
    // use endpointNames somehow ...
    

    看起来效果不错。

    【讨论】:

    • endpointCollection = clientSection.ElementInformation.Properties[string.Empty].Value as ChannelEndpointElementCollection;应该简化为 clientSection.Endpoints;
    【解决方案2】:

    &lt;system.serviceModel&gt; 元素用于配置部分,而不是部分。您需要使用System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup() 来获取整个组。

    【讨论】:

      【解决方案3】:

      感谢@marxidad 的指点,这就是我一直在寻找的东西。

          public static string GetServerName()
          {
              string serverName = "Unknown";
      
              Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
              ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);
              BindingsSection bindings = serviceModel.Bindings;
      
              ChannelEndpointElementCollection endpoints = serviceModel.Client.Endpoints;
      
              for(int i=0; i<endpoints.Count; i++)
              {
                  ChannelEndpointElement endpointElement = endpoints[i];
                  if (endpointElement.Contract == "MyContractName")
                  {
                      serverName = endpointElement.Address.Host;
                  }
              }
      
              return serverName;
          }
      

      【讨论】:

        【解决方案4】:

        GetSectionGroup() 不支持无参数(框架 3.5 下)。

        改为使用:

        Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        ServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
        

        【讨论】:

          【解决方案5】:

          感谢其他发帖者,这是我开发的用于获取命名端点的 URI 的函数。它还会创建一个正在使用的端点列表以及调试时使用的实际配置文件:

          Private Function GetEndpointAddress(name As String) As String
              Debug.Print("--- GetEndpointAddress ---")
              Dim address As String = "Unknown"
              Dim appConfig As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
              Debug.Print("app.config: " & appConfig.FilePath)
              Dim serviceModel As ServiceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup(appConfig)
              Dim bindings As BindingsSection = serviceModel.Bindings
              Dim endpoints As ChannelEndpointElementCollection = serviceModel.Client.Endpoints
              For i As Integer = 0 To endpoints.Count - 1
                  Dim endpoint As ChannelEndpointElement = endpoints(i)
                  Debug.Print("Endpoint: " & endpoint.Name & " - " & endpoint.Address.ToString)
                  If endpoint.Name = name Then
                      address = endpoint.Address.ToString
                  End If
              Next
              Debug.Print("--- GetEndpointAddress ---")
              Return address
          End Function
          

          【讨论】:

            猜你喜欢
            • 2010-09-05
            • 2011-01-07
            • 2015-10-27
            • 2010-12-17
            • 1970-01-01
            • 1970-01-01
            • 2016-04-15
            • 1970-01-01
            相关资源
            最近更新 更多