StandardEndpointsSection.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Reflection;
  9. using System.Runtime;
  10. using System.Security;
  11. using System.ServiceModel;
  12. using System.ServiceModel.Description;
  13. using System.Xml;
  14. using System.Runtime.Diagnostics;
  15. public sealed partial class StandardEndpointsSection : ConfigurationSection, IConfigurationContextProviderInternal
  16. {
  17. static Configuration configuration;
  18. ConfigurationPropertyCollection properties;
  19. public StandardEndpointsSection() { }
  20. Dictionary<string, EndpointCollectionElement> EndpointCollectionElements
  21. {
  22. get
  23. {
  24. Dictionary<string, EndpointCollectionElement> endpointCollectionElements = new Dictionary<string, EndpointCollectionElement>();
  25. foreach (ConfigurationProperty property in this.Properties)
  26. {
  27. endpointCollectionElements.Add(property.Name, this[property.Name]);
  28. }
  29. return endpointCollectionElements;
  30. }
  31. }
  32. new public EndpointCollectionElement this[string endpoint]
  33. {
  34. get
  35. {
  36. return (EndpointCollectionElement)base[endpoint];
  37. }
  38. }
  39. protected override ConfigurationPropertyCollection Properties
  40. {
  41. get
  42. {
  43. if (this.properties == null)
  44. {
  45. this.properties = new ConfigurationPropertyCollection();
  46. }
  47. this.UpdateEndpointSections();
  48. return this.properties;
  49. }
  50. }
  51. [ConfigurationProperty(ConfigurationStrings.MexStandardEndpointCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  52. public ServiceMetadataEndpointCollectionElement MexEndpoint
  53. {
  54. get { return (ServiceMetadataEndpointCollectionElement)base[ConfigurationStrings.MexStandardEndpointCollectionElementName]; }
  55. }
  56. // This property should only be called/set from EndpointsSectionGroup TryAdd
  57. static Configuration Configuration
  58. {
  59. get { return StandardEndpointsSection.configuration; }
  60. set { StandardEndpointsSection.configuration = value; }
  61. }
  62. public static StandardEndpointsSection GetSection(Configuration config)
  63. {
  64. if (config == null)
  65. {
  66. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("config");
  67. }
  68. return (StandardEndpointsSection)config.GetSection(ConfigurationStrings.StandardEndpointsSectionPath);
  69. }
  70. public List<EndpointCollectionElement> EndpointCollections
  71. {
  72. get
  73. {
  74. List<EndpointCollectionElement> endpointCollections = new List<EndpointCollectionElement>();
  75. foreach (ConfigurationProperty property in this.Properties)
  76. {
  77. endpointCollections.Add(this[property.Name]);
  78. }
  79. return endpointCollections;
  80. }
  81. }
  82. internal static bool TryAdd(string name, ServiceEndpoint endpoint, Configuration config, out string endpointSectionName)
  83. {
  84. bool retval = false;
  85. StandardEndpointsSection.Configuration = config;
  86. try
  87. {
  88. retval = StandardEndpointsSection.TryAdd(name, endpoint, out endpointSectionName);
  89. }
  90. finally
  91. {
  92. StandardEndpointsSection.Configuration = null;
  93. }
  94. return retval;
  95. }
  96. protected override bool OnDeserializeUnrecognizedElement(string elementName, XmlReader reader)
  97. {
  98. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  99. new ConfigurationErrorsException(SR.GetString(SR.ConfigEndpointExtensionNotFound,
  100. ConfigurationHelpers.GetEndpointsSectionPath(elementName))));
  101. }
  102. internal static bool TryAdd(string name, ServiceEndpoint endpoint, out string endpointSectionName)
  103. {
  104. // TryAdd built on assumption that StandardEndpointsSectionGroup.Configuration is valid.
  105. // This should be protected at the callers site. If assumption is invalid, then
  106. // configuration system is in an indeterminate state. Need to stop in a manner that
  107. // user code can not capture.
  108. if (null == StandardEndpointsSection.Configuration)
  109. {
  110. Fx.Assert("The TryAdd(string name, ServiceEndpoint endpoint, Configuration config, out string endpointSectionName) variant of this function should always be called first. The Configuration object is not set.");
  111. DiagnosticUtility.FailFast("The TryAdd(string name, ServiceEndpoint endpoint, Configuration config, out string endpointSectionName) variant of this function should always be called first. The Configuration object is not set.");
  112. }
  113. bool retval = false;
  114. string outEndpointSectionName = null;
  115. StandardEndpointsSection sectionGroup = StandardEndpointsSection.GetSection(StandardEndpointsSection.Configuration);
  116. sectionGroup.UpdateEndpointSections();
  117. foreach (string sectionName in sectionGroup.EndpointCollectionElements.Keys)
  118. {
  119. EndpointCollectionElement endpointCollectionElement = sectionGroup.EndpointCollectionElements[sectionName];
  120. MethodInfo tryAddMethod = endpointCollectionElement.GetType().GetMethod("TryAdd", BindingFlags.Instance | BindingFlags.NonPublic);
  121. if (tryAddMethod != null)
  122. {
  123. retval = (bool)tryAddMethod.Invoke(endpointCollectionElement, new object[] { name, endpoint, StandardEndpointsSection.Configuration });
  124. if (retval)
  125. {
  126. outEndpointSectionName = sectionName;
  127. break;
  128. }
  129. }
  130. }
  131. // This little oddity exists to make sure that the out param is assigned to before the method
  132. // exits.
  133. endpointSectionName = outEndpointSectionName;
  134. return retval;
  135. }
  136. void UpdateEndpointSections()
  137. {
  138. UpdateEndpointSections(ConfigurationHelpers.GetEvaluationContext(this));
  139. }
  140. [Fx.Tag.SecurityNote(Critical = "Calls SecurityCritical methods UnsafeLookupCollection which elevates in order to load config.",
  141. Safe = "Does not leak any config objects.")]
  142. [SecuritySafeCritical]
  143. internal void UpdateEndpointSections(ContextInformation evaluationContext)
  144. {
  145. ExtensionElementCollection endpointExtensions = ExtensionsSection.UnsafeLookupCollection(ConfigurationStrings.EndpointExtensions, evaluationContext);
  146. // Extension collections are additive only (BasicMap) and do not allow for <clear>
  147. // or <remove> tags, nor do they allow for overriding an entry. This allows us
  148. // to optimize this to only walk the binding extension collection if the counts
  149. // mismatch.
  150. if (endpointExtensions.Count != this.properties.Count)
  151. {
  152. foreach (ExtensionElement endpointExtension in endpointExtensions)
  153. {
  154. if (null != endpointExtension)
  155. {
  156. if (!this.properties.Contains(endpointExtension.Name))
  157. {
  158. Type extensionType = Type.GetType(endpointExtension.Type, false);
  159. if (extensionType == null)
  160. {
  161. ConfigurationHelpers.TraceExtensionTypeNotFound(endpointExtension);
  162. }
  163. else
  164. {
  165. ConfigurationProperty property = new ConfigurationProperty(endpointExtension.Name,
  166. extensionType,
  167. null,
  168. ConfigurationPropertyOptions.None);
  169. this.properties.Add(property);
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. [Fx.Tag.SecurityNote(Critical = "Calls SecurityCritical methods UnsafeGetAssociatedBindingCollectionElement which elevates in order to load config.",
  177. Safe = "Does not leak any config objects.")]
  178. [SecuritySafeCritical]
  179. internal static void ValidateEndpointReference(string endpoint, string endpointConfiguration, ContextInformation evaluationContext, ConfigurationElement configurationElement)
  180. {
  181. // ValidateEndpointReference built on assumption that evaluationContext is valid.
  182. // This should be protected at the callers site. If assumption is invalid, then
  183. // configuration system is in an indeterminate state. Need to stop in a manner that
  184. // user code can not capture.
  185. if (null == evaluationContext)
  186. {
  187. Fx.Assert("ValidateEndpointReference() should only called with valid ContextInformation");
  188. DiagnosticUtility.FailFast("ValidateEndpointReference() should only called with valid ContextInformation");
  189. }
  190. if (!String.IsNullOrEmpty(endpoint))
  191. {
  192. EndpointCollectionElement endpointCollectionElement = null;
  193. if (null != evaluationContext)
  194. {
  195. endpointCollectionElement = ConfigurationHelpers.UnsafeGetAssociatedEndpointCollectionElement(evaluationContext, endpoint);
  196. }
  197. else
  198. {
  199. endpointCollectionElement = ConfigurationHelpers.UnsafeGetEndpointCollectionElement(endpoint);
  200. }
  201. if (endpointCollectionElement == null)
  202. {
  203. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidSection,
  204. ConfigurationHelpers.GetEndpointsSectionPath(endpoint)),
  205. configurationElement.ElementInformation.Source,
  206. configurationElement.ElementInformation.LineNumber));
  207. }
  208. if (!String.IsNullOrEmpty(endpointConfiguration))
  209. {
  210. if (!endpointCollectionElement.ContainsKey(endpointConfiguration))
  211. {
  212. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidEndpointName,
  213. endpointConfiguration,
  214. ConfigurationHelpers.GetEndpointsSectionPath(endpoint),
  215. ConfigurationStrings.EndpointConfiguration),
  216. configurationElement.ElementInformation.Source,
  217. configurationElement.ElementInformation.LineNumber));
  218. }
  219. }
  220. }
  221. }
  222. ContextInformation IConfigurationContextProviderInternal.GetEvaluationContext()
  223. {
  224. return this.EvaluationContext;
  225. }
  226. [Fx.Tag.SecurityNote(Miscellaneous = "RequiresReview -- the return value will be used for a security decision -- see comment in interface definition.")]
  227. ContextInformation IConfigurationContextProviderInternal.GetOriginalEvaluationContext()
  228. {
  229. Fx.Assert("Not implemented: IConfigurationContextProviderInternal.GetOriginalEvaluationContext");
  230. return null;
  231. }
  232. }
  233. }