简体   繁体   English

使用ConfigurationManager加载System.ServiceModel配置节

[英]Loading System.ServiceModel configuration section using ConfigurationManager

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 ConfigurationManager to 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 ConfigurationManager refuses to load the system.ServiceModel section. 配置部分存在于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. 我想避免手动加载xxx.exe.config文件并使用XPath,但如果我不得不求助于我。 Just seems like a bit of a hack. 看起来有点像黑客。

Any suggestions? 有什么建议?

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. 似乎运作良好。

The <system.serviceModel> element is for a configuration section group , not a section. <system.serviceModel>元素用于配置节 ,而不是节。 You'll need to use System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup() to get the whole group. 您需要使用System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()来获取整个组。

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;
    }

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);

Thanks to the other posters this is the function I developed to get the URI of a named endpoint. 感谢其他海报,这是我开发的用于获取命名端点的URI的函数。 It also creates a listing of the endpoints in use and which actual config file was being used when debugging: 它还会创建正在使用的端点列表以及调试时使用的实际配置文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法读取配置节“ system.ServiceModel”,缺少节声明 - The configuration section 'system.ServiceModel' cannot be read, missing section declaration 为WCF数据服务客户端生成System.ServiceModel配置部分 - Generating the System.ServiceModel configuration section for WCF Data Service client 如何从类库中读取“ System.ServiceModel”配置节组? - How do I read the “System.ServiceModel” configuration section group from a class library? System.ServiceModel消失 - System.ServiceModel disappear System.ServiceModel缺失 - System.ServiceModel missing System.ServiceModel引用 - System.ServiceModel References 为什么是<system.servicemodel> app.config 中缺少部分?</system.servicemodel> - Why is the <system.serviceModel> section missing from app.config? 找不到配置绑定扩展名&#39;system.serviceModel / bindings / pollingDuplexHttpBinding&#39; - Configuration binding extension 'system.serviceModel/bindings/pollingDuplexHttpBinding' could not be found 找不到配置绑定扩展“system.serviceModel/bindings/basicHttpsBinding” - Configuration binding extension 'system.serviceModel/bindings/basicHttpsBinding' could not be found 使用System.ServiceModel时出现System.TypeLoadException - System.TypeLoadException when using System.ServiceModel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM