代码之家  ›  专栏  ›  技术社区  ›  DavidWhitney

使用ConfigurationManager加载System.ServiceModel配置节

  •  59
  • DavidWhitney  · 技术社区  · 16 年前

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

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

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

    似乎总是返回空值。

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

    工作得很好。

    app.config中存在配置部分,但出于某种原因 配置管理器 拒绝加载 system.ServiceModel 部分。

    我希望避免手动加载xxx.exe.config文件并使用xpath,但如果必须使用xpath,我会的。看起来有点像黑客。

    有什么建议吗?

    5 回复  |  直到 10 年前
        1
  •  47
  •   Igor Kustov Vipul    10 年前

    这个 <system.serviceModel> 元素用于配置节 ,不是节。你需要使用 System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup() 让整个团队都参与进来。

        2
  •  58
  •   DavidWhitney    16 年前

    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 ...
    

    看来效果不错。

        3
  •  16
  •   DavidWhitney    16 年前

    这就是我要找的,感谢@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
  •  10
  •   midspace    13 年前

    getSectionGroup()不支持任何参数(在框架3.5下)。

    取而代之的是:

    Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
    
        5
  •  7
  •   Robin G Brown    11 年前

    多亏了其他海报,这是我为获取命名端点的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