ServiceModelExtensionElement.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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.Diagnostics;
  9. using System.Runtime;
  10. using System.Runtime.Diagnostics;
  11. using System.Security;
  12. using System.Security.Permissions;
  13. using System.ServiceModel;
  14. using System.ServiceModel.Diagnostics;
  15. using System.Xml;
  16. [ConfigurationPermission(SecurityAction.InheritanceDemand, Unrestricted = true)]
  17. public abstract class ServiceModelExtensionElement : ServiceModelConfigurationElement, IConfigurationContextProviderInternal
  18. {
  19. [Fx.Tag.SecurityNote(Critical = "Stores information used in a security decision.")]
  20. [SecurityCritical]
  21. EvaluationContextHelper contextHelper;
  22. ContextInformation containingEvaluationContext = null;
  23. string configurationElementName = String.Empty;
  24. string extensionCollectionName = String.Empty;
  25. bool modified = false;
  26. Type thisType;
  27. protected ServiceModelExtensionElement()
  28. : base()
  29. {
  30. }
  31. [Fx.Tag.SecurityNote(Critical = "Calls SecurityCritical method UnsafeLookupCollection which elevates in order to load config.",
  32. Safe = "Does not leak any config objects.")]
  33. [SecuritySafeCritical]
  34. internal bool CanAdd(string extensionCollectionName, ContextInformation evaluationContext)
  35. {
  36. bool retVal = false;
  37. ExtensionElementCollection collection = ExtensionsSection.UnsafeLookupCollection(extensionCollectionName, evaluationContext);
  38. if (null != collection && collection.Count != 0)
  39. {
  40. string thisAssemblyQualifiedName = ThisType.AssemblyQualifiedName;
  41. string thisTypeName = ExtensionElement.GetTypeName(thisAssemblyQualifiedName);
  42. foreach (ExtensionElement extensionElement in collection)
  43. {
  44. string extensionTypeName = extensionElement.Type;
  45. if (extensionTypeName.Equals(thisAssemblyQualifiedName, StringComparison.Ordinal))
  46. {
  47. retVal = true;
  48. break;
  49. }
  50. if (extensionElement.TypeName.Equals(thisTypeName, StringComparison.Ordinal))
  51. {
  52. Type extensionType = Type.GetType(extensionTypeName, false);
  53. if (extensionType != null && extensionType.Equals(ThisType))
  54. {
  55. retVal = true;
  56. break;
  57. }
  58. }
  59. }
  60. if (!retVal && DiagnosticUtility.ShouldTraceWarning)
  61. {
  62. TraceUtility.TraceEvent(TraceEventType.Warning,
  63. TraceCode.ConfiguredExtensionTypeNotFound,
  64. SR.GetString(SR.TraceCodeConfiguredExtensionTypeNotFound),
  65. this.CreateCanAddRecord(extensionCollectionName), this, null);
  66. }
  67. }
  68. else if (DiagnosticUtility.ShouldTraceWarning)
  69. {
  70. int traceCode;
  71. string traceDescription;
  72. if (collection != null && collection.Count == 0)
  73. {
  74. traceCode = TraceCode.ExtensionCollectionIsEmpty;
  75. traceDescription = SR.GetString(SR.TraceCodeExtensionCollectionIsEmpty);
  76. }
  77. else
  78. {
  79. traceCode = TraceCode.ExtensionCollectionDoesNotExist;
  80. traceDescription = SR.GetString(SR.TraceCodeExtensionCollectionDoesNotExist);
  81. }
  82. TraceUtility.TraceEvent(TraceEventType.Warning,
  83. traceCode, traceDescription, this.CreateCanAddRecord(extensionCollectionName), this, null);
  84. }
  85. return retVal;
  86. }
  87. public string ConfigurationElementName
  88. {
  89. get
  90. {
  91. if (String.IsNullOrEmpty(this.configurationElementName))
  92. {
  93. this.configurationElementName = this.GetConfigurationElementName();
  94. }
  95. return this.configurationElementName;
  96. }
  97. internal set
  98. {
  99. if (!string.IsNullOrEmpty(this.configurationElementName))
  100. {
  101. Fx.Assert(this.configurationElementName == value,
  102. string.Format(System.Globalization.CultureInfo.InvariantCulture,
  103. "The configuration element name has already being set to '{0} and cannot be reset to '{1}'",
  104. this.configurationElementName, value));
  105. return;
  106. }
  107. this.configurationElementName = value;
  108. }
  109. }
  110. internal ContextInformation ContainingEvaluationContext
  111. {
  112. get { return this.containingEvaluationContext; }
  113. set { this.containingEvaluationContext = value; }
  114. }
  115. Type ThisType
  116. {
  117. get
  118. {
  119. if (thisType == null)
  120. {
  121. thisType = this.GetType();
  122. }
  123. return thisType;
  124. }
  125. }
  126. public virtual void CopyFrom(ServiceModelExtensionElement from)
  127. {
  128. if (this.IsReadOnly())
  129. {
  130. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigReadOnly)));
  131. }
  132. if (from == null)
  133. {
  134. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("from");
  135. }
  136. }
  137. DictionaryTraceRecord CreateCanAddRecord(string extensionCollectionName)
  138. {
  139. Dictionary<string, string> values = new Dictionary<string, string>(2);
  140. values["ElementType"] = System.Runtime.Diagnostics.DiagnosticTraceBase.XmlEncode(ThisType.AssemblyQualifiedName);
  141. values["CollectionName"] = ConfigurationStrings.ExtensionsSectionPath + "/" + extensionCollectionName;
  142. return new DictionaryTraceRecord(values);
  143. }
  144. internal void DeserializeInternal(XmlReader reader, bool serializeCollectionKey)
  145. {
  146. this.DeserializeElement(reader, serializeCollectionKey);
  147. }
  148. internal string ExtensionCollectionName
  149. {
  150. set { this.extensionCollectionName = value; }
  151. get { return this.extensionCollectionName; }
  152. }
  153. internal ContextInformation EvalContext
  154. {
  155. get { return this.EvaluationContext; }
  156. }
  157. internal object FromProperty(ConfigurationProperty property)
  158. {
  159. return this[property];
  160. }
  161. [Fx.Tag.SecurityNote(Critical = "Calls SecurityCritical methods UnsafeLookupCollection and UnsafeLookupAssociatedCollection which elevate in order to load config.",
  162. Safe = "Does not leak any config objects.")]
  163. [SecuritySafeCritical]
  164. string GetConfigurationElementName()
  165. {
  166. string configurationElementName = String.Empty;
  167. ExtensionElementCollection collection = null;
  168. Type extensionSectionType = ThisType;
  169. ContextInformation evaluationContext = this.ContainingEvaluationContext;
  170. if (evaluationContext == null)
  171. {
  172. evaluationContext = ConfigurationHelpers.GetEvaluationContext(this);
  173. }
  174. if (String.IsNullOrEmpty(this.extensionCollectionName))
  175. {
  176. if (DiagnosticUtility.ShouldTraceWarning)
  177. {
  178. TraceUtility.TraceEvent(TraceEventType.Warning,
  179. TraceCode.ExtensionCollectionNameNotFound,
  180. SR.GetString(SR.TraceCodeExtensionCollectionNameNotFound),
  181. this,
  182. (Exception)null);
  183. }
  184. collection = ExtensionsSection.UnsafeLookupAssociatedCollection(ThisType, evaluationContext, out this.extensionCollectionName);
  185. }
  186. else
  187. {
  188. collection = ExtensionsSection.UnsafeLookupCollection(this.extensionCollectionName, evaluationContext);
  189. }
  190. if (null == collection)
  191. {
  192. if (String.IsNullOrEmpty(this.extensionCollectionName))
  193. {
  194. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigNoExtensionCollectionAssociatedWithType,
  195. extensionSectionType.AssemblyQualifiedName),
  196. this.ElementInformation.Source,
  197. this.ElementInformation.LineNumber));
  198. }
  199. else
  200. {
  201. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigExtensionCollectionNotFound,
  202. this.extensionCollectionName),
  203. this.ElementInformation.Source,
  204. this.ElementInformation.LineNumber));
  205. }
  206. }
  207. for (int i = 0; i < collection.Count; i++)
  208. {
  209. ExtensionElement collectionElement = collection[i];
  210. // Optimize for assembly qualified names.
  211. if (collectionElement.Type.Equals(extensionSectionType.AssemblyQualifiedName, StringComparison.Ordinal))
  212. {
  213. configurationElementName = collectionElement.Name;
  214. break;
  215. }
  216. // Check type directly for the case that the extension is registered with something less than
  217. // an full assembly qualified name.
  218. Type collectionElementType = Type.GetType(collectionElement.Type, false);
  219. if (null != collectionElementType && extensionSectionType.Equals(collectionElementType))
  220. {
  221. configurationElementName = collectionElement.Name;
  222. break;
  223. }
  224. }
  225. if (String.IsNullOrEmpty(configurationElementName))
  226. {
  227. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigExtensionTypeNotRegisteredInCollection,
  228. extensionSectionType.AssemblyQualifiedName,
  229. this.extensionCollectionName),
  230. this.ElementInformation.Source,
  231. this.ElementInformation.LineNumber));
  232. }
  233. return configurationElementName;
  234. }
  235. internal void InternalInitializeDefault()
  236. {
  237. this.InitializeDefault();
  238. }
  239. protected override bool IsModified()
  240. {
  241. return this.modified | base.IsModified();
  242. }
  243. internal bool IsModifiedInternal()
  244. {
  245. return this.IsModified();
  246. }
  247. internal ConfigurationPropertyCollection PropertiesInternal
  248. {
  249. get { return this.Properties; }
  250. }
  251. internal void ResetModifiedInternal()
  252. {
  253. this.ResetModified();
  254. }
  255. protected override bool SerializeElement(XmlWriter writer, bool serializeCollectionKey)
  256. {
  257. base.SerializeElement(writer, serializeCollectionKey);
  258. return true;
  259. }
  260. internal bool SerializeInternal(XmlWriter writer, bool serializeCollectionKey)
  261. {
  262. return this.SerializeElement(writer, serializeCollectionKey);
  263. }
  264. internal void SetReadOnlyInternal()
  265. {
  266. this.SetReadOnly();
  267. }
  268. [Fx.Tag.SecurityNote(Critical = "Accesses critical field contextHelper.")]
  269. [SecurityCritical]
  270. protected override void Reset(ConfigurationElement parentElement)
  271. {
  272. this.contextHelper.OnReset(parentElement);
  273. base.Reset(parentElement);
  274. }
  275. ContextInformation IConfigurationContextProviderInternal.GetEvaluationContext()
  276. {
  277. return this.EvaluationContext;
  278. }
  279. [Fx.Tag.SecurityNote(Critical = "Accesses critical field contextHelper.",
  280. Miscellaneous = "RequiresReview -- the return value will be used for a security decision -- see comment in interface definition.")]
  281. [SecurityCritical]
  282. ContextInformation IConfigurationContextProviderInternal.GetOriginalEvaluationContext()
  283. {
  284. return this.contextHelper.GetOriginalContext(this);
  285. }
  286. }
  287. }