当前位置:  开发笔记 > 编程语言 > 正文

使用ConfigurationManager加载System.ServiceModel配置节

如何解决《使用ConfigurationManager加载System.ServiceModel配置节》经验,为你挑选了5个好方法。

使用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,但如果我不得不求助于我.看起来有点像黑客.

有什么建议?



1> DavidWhitney..:

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 endpointNames = new List();
foreach (ChannelEndpointElement endpointElement in endpointCollection)
{
    endpointNames.Add(endpointElement.Name);
}
// use endpointNames somehow ...

似乎运作良好.



2> Mark Cidade..:

元素用于配置节,而不是节.你需要用来System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()获得整个团队.



3> DavidWhitney..:

这是我正在寻找的感谢@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



4> midspace..:

GetSectionGroup()不支持任何参数(在框架3.5下).

而是使用:

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



5> Robin G Brow..:

感谢其他海报,这是我开发的用于获取命名端点的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

推荐阅读
大大炮
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有