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