BindingsSection.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.Channels;
  13. using System.Xml;
  14. using System.Runtime.Diagnostics;
  15. public sealed partial class BindingsSection : ConfigurationSection, IConfigurationContextProviderInternal
  16. {
  17. static Configuration configuration;
  18. ConfigurationPropertyCollection properties;
  19. public BindingsSection() { }
  20. Dictionary<string, BindingCollectionElement> BindingCollectionElements
  21. {
  22. get
  23. {
  24. Dictionary<string, BindingCollectionElement> bindingCollectionElements = new Dictionary<string, BindingCollectionElement>();
  25. foreach (ConfigurationProperty property in this.Properties)
  26. {
  27. bindingCollectionElements.Add(property.Name, this[property.Name]);
  28. }
  29. return bindingCollectionElements;
  30. }
  31. }
  32. new public BindingCollectionElement this[string binding]
  33. {
  34. get
  35. {
  36. return (BindingCollectionElement)base[binding];
  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.UpdateBindingSections();
  48. return this.properties;
  49. }
  50. }
  51. [ConfigurationProperty(ConfigurationStrings.BasicHttpBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  52. public BasicHttpBindingCollectionElement BasicHttpBinding
  53. {
  54. get { return (BasicHttpBindingCollectionElement)base[ConfigurationStrings.BasicHttpBindingCollectionElementName]; }
  55. }
  56. [ConfigurationProperty(ConfigurationStrings.BasicHttpsBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  57. public BasicHttpsBindingCollectionElement BasicHttpsBinding
  58. {
  59. get { return (BasicHttpsBindingCollectionElement)base[ConfigurationStrings.BasicHttpsBindingCollectionElementName]; }
  60. }
  61. // This property should only be called/set from BindingsSectionGroup TryAdd
  62. static Configuration Configuration
  63. {
  64. get { return BindingsSection.configuration; }
  65. set { BindingsSection.configuration = value; }
  66. }
  67. [ConfigurationProperty(ConfigurationStrings.CustomBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  68. public CustomBindingCollectionElement CustomBinding
  69. {
  70. get { return (CustomBindingCollectionElement)base[ConfigurationStrings.CustomBindingCollectionElementName]; }
  71. }
  72. [ConfigurationProperty(ConfigurationStrings.MsmqIntegrationBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  73. public MsmqIntegrationBindingCollectionElement MsmqIntegrationBinding
  74. {
  75. get { return (MsmqIntegrationBindingCollectionElement)base[ConfigurationStrings.MsmqIntegrationBindingCollectionElementName]; }
  76. }
  77. [ConfigurationProperty(ConfigurationStrings.NetHttpBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  78. public NetHttpBindingCollectionElement NetHttpBinding
  79. {
  80. get { return (NetHttpBindingCollectionElement)base[ConfigurationStrings.NetHttpBindingCollectionElementName]; }
  81. }
  82. [ConfigurationProperty(ConfigurationStrings.NetHttpsBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  83. public NetHttpsBindingCollectionElement NetHttpsBinding
  84. {
  85. get { return (NetHttpsBindingCollectionElement)base[ConfigurationStrings.NetHttpsBindingCollectionElementName]; }
  86. }
  87. [ConfigurationProperty(ConfigurationStrings.NetPeerTcpBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  88. [ObsoleteAttribute ("PeerChannel feature is obsolete and will be removed in the future.", false)]
  89. public NetPeerTcpBindingCollectionElement NetPeerTcpBinding
  90. {
  91. get { return (NetPeerTcpBindingCollectionElement)base[ConfigurationStrings.NetPeerTcpBindingCollectionElementName]; }
  92. }
  93. [ConfigurationProperty(ConfigurationStrings.NetMsmqBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  94. public NetMsmqBindingCollectionElement NetMsmqBinding
  95. {
  96. get { return (NetMsmqBindingCollectionElement)base[ConfigurationStrings.NetMsmqBindingCollectionElementName]; }
  97. }
  98. [ConfigurationProperty(ConfigurationStrings.NetNamedPipeBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  99. public NetNamedPipeBindingCollectionElement NetNamedPipeBinding
  100. {
  101. get { return (NetNamedPipeBindingCollectionElement)base[ConfigurationStrings.NetNamedPipeBindingCollectionElementName]; }
  102. }
  103. [ConfigurationProperty(ConfigurationStrings.NetTcpBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  104. public NetTcpBindingCollectionElement NetTcpBinding
  105. {
  106. get { return (NetTcpBindingCollectionElement)base[ConfigurationStrings.NetTcpBindingCollectionElementName]; }
  107. }
  108. [ConfigurationProperty(ConfigurationStrings.WSFederationHttpBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  109. public WSFederationHttpBindingCollectionElement WSFederationHttpBinding
  110. {
  111. get { return (WSFederationHttpBindingCollectionElement)base[ConfigurationStrings.WSFederationHttpBindingCollectionElementName]; }
  112. }
  113. [ConfigurationProperty(ConfigurationStrings.WS2007FederationHttpBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  114. public WS2007FederationHttpBindingCollectionElement WS2007FederationHttpBinding
  115. {
  116. get { return (WS2007FederationHttpBindingCollectionElement)base[ConfigurationStrings.WS2007FederationHttpBindingCollectionElementName]; }
  117. }
  118. [ConfigurationProperty(ConfigurationStrings.WSHttpBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  119. public WSHttpBindingCollectionElement WSHttpBinding
  120. {
  121. get { return (WSHttpBindingCollectionElement)base[ConfigurationStrings.WSHttpBindingCollectionElementName]; }
  122. }
  123. [ConfigurationProperty(ConfigurationStrings.WS2007HttpBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  124. public WS2007HttpBindingCollectionElement WS2007HttpBinding
  125. {
  126. get { return (WS2007HttpBindingCollectionElement)base[ConfigurationStrings.WS2007HttpBindingCollectionElementName]; }
  127. }
  128. [ConfigurationProperty(ConfigurationStrings.WSDualHttpBindingCollectionElementName, Options = ConfigurationPropertyOptions.None)]
  129. public WSDualHttpBindingCollectionElement WSDualHttpBinding
  130. {
  131. get { return (WSDualHttpBindingCollectionElement)base[ConfigurationStrings.WSDualHttpBindingCollectionElementName]; }
  132. }
  133. public static BindingsSection GetSection(Configuration config)
  134. {
  135. if (config == null)
  136. {
  137. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("config");
  138. }
  139. return (BindingsSection)config.GetSection(ConfigurationStrings.BindingsSectionGroupPath);
  140. }
  141. public List<BindingCollectionElement> BindingCollections
  142. {
  143. get
  144. {
  145. List<BindingCollectionElement> bindingCollections = new List<BindingCollectionElement>();
  146. foreach (ConfigurationProperty property in this.Properties)
  147. {
  148. bindingCollections.Add(this[property.Name]);
  149. }
  150. return bindingCollections;
  151. }
  152. }
  153. protected override bool OnDeserializeUnrecognizedElement(string elementName, XmlReader reader)
  154. {
  155. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  156. new ConfigurationErrorsException(SR.GetString(SR.ConfigBindingExtensionNotFound,
  157. ConfigurationHelpers.GetBindingsSectionPath(elementName))));
  158. }
  159. internal static bool TryAdd(string name, Binding binding, Configuration config, out string bindingSectionName)
  160. {
  161. bool retval = false;
  162. BindingsSection.Configuration = config;
  163. try
  164. {
  165. retval = BindingsSection.TryAdd(name, binding, out bindingSectionName);
  166. }
  167. finally
  168. {
  169. BindingsSection.Configuration = null;
  170. }
  171. return retval;
  172. }
  173. internal static bool TryAdd(string name, Binding binding, out string bindingSectionName)
  174. {
  175. // TryAdd built on assumption that BindingsSectionGroup.Configuration is valid.
  176. // This should be protected at the callers site. If assumption is invalid, then
  177. // configuration system is in an indeterminate state. Need to stop in a manner that
  178. // user code can not capture.
  179. if (null == BindingsSection.Configuration)
  180. {
  181. Fx.Assert("The TryAdd(string name, Binding binding, Configuration config, out string binding) variant of this function should always be called first. The Configuration object is not set.");
  182. DiagnosticUtility.FailFast("The TryAdd(string name, Binding binding, Configuration config, out string binding) variant of this function should always be called first. The Configuration object is not set.");
  183. }
  184. bool retval = false;
  185. string outBindingSectionName = null;
  186. BindingsSection sectionGroup = BindingsSection.GetSection(BindingsSection.Configuration);
  187. sectionGroup.UpdateBindingSections();
  188. foreach (string sectionName in sectionGroup.BindingCollectionElements.Keys)
  189. {
  190. BindingCollectionElement bindingCollectionElement = sectionGroup.BindingCollectionElements[sectionName];
  191. // Save the custom bindings as the last choice
  192. if (!(bindingCollectionElement is CustomBindingCollectionElement))
  193. {
  194. MethodInfo tryAddMethod = bindingCollectionElement.GetType().GetMethod("TryAdd", BindingFlags.Instance | BindingFlags.NonPublic);
  195. if (tryAddMethod != null)
  196. {
  197. retval = (bool)tryAddMethod.Invoke(bindingCollectionElement, new object[] { name, binding, BindingsSection.Configuration });
  198. if (retval)
  199. {
  200. outBindingSectionName = sectionName;
  201. break;
  202. }
  203. }
  204. }
  205. }
  206. if (!retval)
  207. {
  208. // Much of the time, the custombinding should come out ok.
  209. CustomBindingCollectionElement customBindingSection = CustomBindingCollectionElement.GetBindingCollectionElement();
  210. retval = customBindingSection.TryAdd(name, binding, BindingsSection.Configuration);
  211. if (retval)
  212. {
  213. outBindingSectionName = ConfigurationStrings.CustomBindingCollectionElementName;
  214. }
  215. }
  216. // This little oddity exists to make sure that the out param is assigned to before the method
  217. // exits.
  218. bindingSectionName = outBindingSectionName;
  219. return retval;
  220. }
  221. void UpdateBindingSections()
  222. {
  223. UpdateBindingSections(ConfigurationHelpers.GetEvaluationContext(this));
  224. }
  225. [Fx.Tag.SecurityNote(Critical = "Calls UnsafeLookupCollection which elevates.",
  226. Safe = "Doesn't leak resultant config.")]
  227. [SecuritySafeCritical]
  228. internal void UpdateBindingSections(ContextInformation evaluationContext)
  229. {
  230. ExtensionElementCollection bindingExtensions = ExtensionsSection.UnsafeLookupCollection(ConfigurationStrings.BindingExtensions, evaluationContext);
  231. // Extension collections are additive only (BasicMap) and do not allow for <clear>
  232. // or <remove> tags, nor do they allow for overriding an entry. This allows us
  233. // to optimize this to only walk the binding extension collection if the counts
  234. // mismatch.
  235. if (bindingExtensions.Count != this.properties.Count)
  236. {
  237. foreach (ExtensionElement bindingExtension in bindingExtensions)
  238. {
  239. if (null != bindingExtension)
  240. {
  241. if (!this.properties.Contains(bindingExtension.Name))
  242. {
  243. Type extensionType = Type.GetType(bindingExtension.Type, false);
  244. if (extensionType == null)
  245. {
  246. ConfigurationHelpers.TraceExtensionTypeNotFound(bindingExtension);
  247. }
  248. else
  249. {
  250. ConfigurationProperty property = new ConfigurationProperty(bindingExtension.Name,
  251. extensionType,
  252. null,
  253. ConfigurationPropertyOptions.None);
  254. this.properties.Add(property);
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }
  261. [Fx.Tag.SecurityNote(Critical = "Calls UnsafeGetAssociatedBindingCollectionElement which elevates.",
  262. Safe = "Doesn't leak resultant config.")]
  263. [SecuritySafeCritical]
  264. internal static void ValidateBindingReference(string binding, string bindingConfiguration, ContextInformation evaluationContext, ConfigurationElement configurationElement)
  265. {
  266. // ValidateBindingReference built on assumption that evaluationContext is valid.
  267. // This should be protected at the callers site. If assumption is invalid, then
  268. // configuration system is in an indeterminate state. Need to stop in a manner that
  269. // user code can not capture.
  270. if (null == evaluationContext)
  271. {
  272. Fx.Assert("ValidateBindingReference() should only called with valid ContextInformation");
  273. DiagnosticUtility.FailFast("ValidateBindingReference() should only called with valid ContextInformation");
  274. }
  275. if (!String.IsNullOrEmpty(binding))
  276. {
  277. BindingCollectionElement bindingCollectionElement = null;
  278. if (null != evaluationContext)
  279. {
  280. bindingCollectionElement = ConfigurationHelpers.UnsafeGetAssociatedBindingCollectionElement(evaluationContext, binding);
  281. }
  282. else
  283. {
  284. bindingCollectionElement = ConfigurationHelpers.UnsafeGetBindingCollectionElement(binding);
  285. }
  286. if (bindingCollectionElement == null)
  287. {
  288. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidSection,
  289. ConfigurationHelpers.GetBindingsSectionPath(binding)),
  290. configurationElement.ElementInformation.Source,
  291. configurationElement.ElementInformation.LineNumber));
  292. }
  293. if (!String.IsNullOrEmpty(bindingConfiguration))
  294. {
  295. if (!bindingCollectionElement.ContainsKey(bindingConfiguration))
  296. {
  297. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidBindingName,
  298. bindingConfiguration,
  299. ConfigurationHelpers.GetBindingsSectionPath(binding),
  300. ConfigurationStrings.BindingConfiguration),
  301. configurationElement.ElementInformation.Source,
  302. configurationElement.ElementInformation.LineNumber));
  303. }
  304. }
  305. }
  306. }
  307. ContextInformation IConfigurationContextProviderInternal.GetEvaluationContext()
  308. {
  309. return this.EvaluationContext;
  310. }
  311. [Fx.Tag.SecurityNote(Miscellaneous = "RequiresReview - the return value will be used for a security decision -- see comment in interface definition.")]
  312. ContextInformation IConfigurationContextProviderInternal.GetOriginalEvaluationContext()
  313. {
  314. Fx.Assert("Not implemented: IConfigurationContextProviderInternal.GetOriginalEvaluationContext");
  315. return null;
  316. }
  317. }
  318. }