code_junkie

Loading System.ServiceModel configuration section using ConfigurationManager

Nov 10th, 2011
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel");
  2.  
  3. var serviceModelSection = ConfigurationManager.GetSection("appSettings");
  4.  
  5. // Automagically find all client endpoints defined in app.config
  6. ClientSection clientSection =
  7. ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
  8.  
  9. ChannelEndpointElementCollection endpointCollection =
  10. clientSection.ElementInformation.Properties[string.Empty].Value as ChannelEndpointElementCollection;
  11. List<string> endpointNames = new List<string>();
  12. foreach (ChannelEndpointElement endpointElement in endpointCollection)
  13. {
  14. endpointNames.Add(endpointElement.Name);
  15. }
  16. // use endpointNames somehow ...
  17.  
  18. public static string GetServerName()
  19. {
  20. string serverName = "Unknown";
  21.  
  22. Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  23. ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);
  24. BindingsSection bindings = serviceModel.Bindings;
  25.  
  26. ChannelEndpointElementCollection endpoints = serviceModel.Client.Endpoints;
  27.  
  28. for(int i=0; i<endpoints.Count; i++)
  29. {
  30. ChannelEndpointElement endpointElement = endpoints[i];
  31. if (endpointElement.Contract == "MyContractName")
  32. {
  33. serverName = endpointElement.Address.Host;
  34. }
  35. }
  36.  
  37. return serverName;
  38. }
  39.  
  40. Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  41. ServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
Add Comment
Please, Sign In to add comment