C# 使用 ConfigurationManager 加载 System.ServiceModel 配置部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:03:17  来源:igfitidea点击:

Loading System.ServiceModel configuration section using ConfigurationManager

提问by DavidWhitney

Using C# .NET 3.5 and WCF, I'm trying to write out some of the WCF configuration in a client application (the name of the server the client is connecting to).

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

The obvious way is to use ConfigurationManagerto load the configuration section and write out the data I need.

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

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

Appears to always return null.

似乎总是返回 null。

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

Works perfectly.

完美运行。

The configuration section is present in the App.config but for some reason ConfigurationManagerrefuses to load the system.ServiceModelsection.

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

I want to avoid manually loading the xxx.exe.config file and using XPath but if I have to resort to that I will. Just seems like a bit of a hack.

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

Any suggestions?

有什么建议?

采纳答案by Mark Cidade

The <system.serviceModel>element is for a configuration section group, not a section. You'll need to use System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()to get the whole group.

<system.serviceModel>元素用于配置节,而不是节。您需要使用System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()来获取整个组。

回答by DavidWhitney

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

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

Appears to work well.

似乎运作良好。

回答by DavidWhitney

This is what I was looking for thanks to @marxidad for the pointer.

由于@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;
    }

回答by midspace

GetSectionGroup() does not support no parameters (under framework 3.5).

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

Instead use:

而是使用:

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

回答by Robin G Brown

Thanks to the other posters this is the function I developed to get the URI of a named endpoint. It also creates a listing of the endpoints in use and which actual config file was being used when debugging:

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