Sistema de Carga.Sección de configuración de ServiceModel usando ConfigurationManager


Usando C#. NET 3.5 y WCF, estoy tratando de escribir parte de la configuración de WCF en una aplicación cliente (el nombre del servidor al que se está conectando el cliente).

La forma obvia es usar ConfigurationManager para cargar la sección de configuración y escribir los datos que necesito.

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

Siempre devuelve null.

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

Funciona perfectamente.

La sección de configuración está presente en la aplicación.pero por alguna razón ConfigurationManager se niega a cargar el system.ServiceModel apartado.

Quiero evitar cargar manualmente el xxx.exe.config file y usando XPath pero si tengo que recurrir a eso lo haré. Parece un poco pirateado.

Alguna sugerencia?

Author: davmos, 2008-08-21

5 answers

El <system.serviceModel> el elemento es para una sección de configuración group , no una sección. Usted tendrá que utilizar System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup() para conseguir a todo el grupo.

 47
Author: Mark Cidade,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-02 06:18:11

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

Parece funcionar bien.

 56
Author: DavidWhitney,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-21 10:35:08

Esto es lo que estaba buscando gracias a @marxidad por el puntero.

    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;
    }
 16
Author: DavidWhitney,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-21 10:51:07

GetSectionGroup() no soporta ningún parámetro (bajo framework 3.5).

En su lugar use:

Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
 10
Author: midspace,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-13 02:19:35

Gracias a los otros posters esta es la función que desarrollé para obtener el URI de un endpoint con nombre. También crea una lista de los endpoints en uso y qué archivo de configuración real se estaba utilizando al depurar:

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
 7
Author: Robin G Brown,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-17 08:34:13