Loading System.ServiceModel configuration section using ConfigurationManager

59,269

Solution 1

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.

Solution 2

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.

Solution 3

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

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

Solution 4

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

Instead use:

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

Solution 5

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:

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
Share:
59,269
DavidWhitney
Author by

DavidWhitney

David is the founder of Electric Head Software, working on an independent software consultant based in London focusing on IT software delivery, developer mentoring and cultural change - mostly working with London-based organizations and start-ups. David has previously served as the Technical Architect for JustGiving, and helped market-leading organizations such as JUST-EAT, Trainline, Euromoney and Vodafone improve their technical capabilities and culture across a variety of principal engineering and senior leadership roles. David is also the best-selling author of the book "Get Coding!" - a childrens programming book available worldwide, and it's successor Get Coding 2! - covering videogame programming for 9-14 year olds. There's a chance you've seen him talk at a spread of conferences, usergroups and code-dojos around the UK over the last decade, or indulged in bar-room programming debates after one. You can find his open source projects on NuGet and GitHub, follow him on Twitter @david_whitney, or check out his technical blog at http://www.davidwhitney.co.uk/Blog.

Updated on July 05, 2022

Comments

  • DavidWhitney
    DavidWhitney almost 2 years

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

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

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

    Appears to always return 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.

    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.

    Any suggestions?

  • joedotnot
    joedotnot over 10 years
    the confusing line for endpointCollection = clientSection.ElementInformation.Properties[string.Empty].Va‌​lue as ChannelEndpointElementCollection; should be simplified to clientSection.Endpoints;