Chargement de la section de configuration System.ServiceModel à l'aide de ConfigurationManager
-
09-06-2019 - |
Question
En utilisant C# .NET 3.5 et WCF, j'essaie d'écrire une partie de la configuration WCF dans une application client (le nom du serveur auquel le client se connecte).
La manière la plus évidente est d'utiliser ConfigurationManager
pour charger la section de configuration et écrire les données dont j'ai besoin.
var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel");
Semble toujours renvoyer null.
var serviceModelSection = ConfigurationManager.GetSection("appSettings");
Fonctionne parfaitement.
La section de configuration est présente dans App.config mais pour une raison quelconque ConfigurationManager
refuse de charger le system.ServiceModel
section.
Je veux éviter de charger manuellement le fichier xxx.exe.config et d'utiliser XPath, mais si je dois y recourir, je le ferai.Cela semble être un peu un hack.
Aucune suggestion?
La solution
Le <system.serviceModel>
l'élément est destiné à une section de configuration groupe, pas une section.Vous devrez utiliser System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()
pour réunir tout le groupe.
Autres conseils
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 ...
Semble bien fonctionner.
C'est ce que je cherchais grâce à @marxidad pour le pointeur.
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() ne prend en charge aucun paramètre (sous framework 3.5).
Utilisez plutôt :
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
Grâce aux autres affiches, c'est la fonction que j'ai développée pour obtenir l'URI d'un point de terminaison nommé.Il crée également une liste des points de terminaison utilisés et quel fichier de configuration réel a été utilisé lors du débogage :
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