Loading System.ServiceModel configuration section using ConfigurationManager

前端 未结 5 1591
借酒劲吻你
借酒劲吻你 2020-12-07 18:30

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 o

相关标签:
5条回答
  • 2020-12-07 19:04

    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.

    0 讨论(0)
  • 2020-12-07 19:05

    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);
    
    0 讨论(0)
  • 2020-12-07 19:07

    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.

    0 讨论(0)
  • 2020-12-07 19:11

    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
    
    0 讨论(0)
  • 2020-12-07 19:17

    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;
        }
    
    0 讨论(0)
提交回复
热议问题