public virtual bool CanAdd(TServiceModelExtensionElement element)
        {
            if (element == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("element");
            }
            bool flag        = false;
            Type elementType = element.GetType();

            if (!this.IsReadOnly())
            {
                if (!this.ContainsKey(elementType))
                {
                    return(element.CanAdd(this.extensionCollectionName, ConfigurationHelpers.GetEvaluationContext(this)));
                }
                if (DiagnosticUtility.ShouldTraceWarning)
                {
                    TraceUtility.TraceEvent(TraceEventType.Warning, 0x8001d, System.ServiceModel.SR.GetString("TraceCodeExtensionElementAlreadyExistsInCollection"), this.CreateCanAddRecord(this[elementType]), this, null);
                }
                return(flag);
            }
            if (DiagnosticUtility.ShouldTraceWarning)
            {
                TraceUtility.TraceEvent(TraceEventType.Warning, 0x80016, System.ServiceModel.SR.GetString("TraceCodeConfigurationIsReadOnly"), null, this, null);
            }
            return(flag);
        }
        void ValidateSection()
        {
            ContextInformation context = ConfigurationHelpers.GetEvaluationContext(this);

            if (context != null)
            {
                foreach (ServiceElement service in this.Services)
                {
                    BehaviorsSection.ValidateServiceBehaviorReference(service.BehaviorConfiguration, context, service);

                    foreach (ServiceEndpointElement endpoint in service.Endpoints)
                    {
                        if (string.IsNullOrEmpty(endpoint.Kind))
                        {
                            if (!string.IsNullOrEmpty(endpoint.EndpointConfiguration))
                            {
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidAttribute, "endpointConfiguration", "endpoint", "kind")));
                            }
                            if (string.IsNullOrEmpty(endpoint.Binding))
                            {
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.RequiredAttributeMissing, "binding", "endpoint")));
                            }
                        }
                        if (string.IsNullOrEmpty(endpoint.Binding) && !string.IsNullOrEmpty(endpoint.BindingConfiguration))
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidAttribute, "bindingConfiguration", "endpoint", "binding")));
                        }
                        BehaviorsSection.ValidateEndpointBehaviorReference(endpoint.BehaviorConfiguration, context, endpoint);
                        BindingsSection.ValidateBindingReference(endpoint.Binding, endpoint.BindingConfiguration, context, endpoint);
                        StandardEndpointsSection.ValidateEndpointReference(endpoint.Kind, endpoint.EndpointConfiguration, context, endpoint);
                    }
                }
            }
        }
        public virtual void Add(TServiceModelExtensionElement element)
        {
            if (this.IsReadOnly())
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigReadOnly)));
            }
            if (element == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("element");
            }

            element.ExtensionCollectionName = this.extensionCollectionName;

            if (this.Contains(element))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("element", SR.GetString(SR.ConfigDuplicateKey, element.ConfigurationElementName));
            }
            else if (!this.CanAdd(element))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("element",
                                                                             SR.GetString(SR.ConfigElementTypeNotAllowed,
                                                                                          element.ConfigurationElementName,
                                                                                          this.extensionCollectionName));
            }
            else
            {
                element.ContainingEvaluationContext = ConfigurationHelpers.GetEvaluationContext(this);
                ConfigurationProperty configProperty = new ConfigurationProperty(element.ConfigurationElementName, element.GetType(), null);
                this.Properties.Add(configProperty);
                this[configProperty] = element;
                this.Items.Add(element);
                this.modified = true;
            }
        }
Beispiel #4
0
        private void ValidateSection()
        {
            ContextInformation evaluationContext = ConfigurationHelpers.GetEvaluationContext(this);

            if (evaluationContext != null)
            {
                foreach (ChannelEndpointElement element in this.Endpoints)
                {
                    if (string.IsNullOrEmpty(element.Kind))
                    {
                        if (!string.IsNullOrEmpty(element.EndpointConfiguration))
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigInvalidAttribute", new object[] { "endpointConfiguration", "endpoint", "kind" })));
                        }
                        if (string.IsNullOrEmpty(element.Binding))
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("RequiredAttributeMissing", new object[] { "binding", "endpoint" })));
                        }
                        if (string.IsNullOrEmpty(element.Contract))
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("RequiredAttributeMissing", new object[] { "contract", "endpoint" })));
                        }
                    }
                    if (string.IsNullOrEmpty(element.Binding) && !string.IsNullOrEmpty(element.BindingConfiguration))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigInvalidAttribute", new object[] { "bindingConfiguration", "endpoint", "binding" })));
                    }
                    BehaviorsSection.ValidateEndpointBehaviorReference(element.BehaviorConfiguration, evaluationContext, element);
                    BindingsSection.ValidateBindingReference(element.Binding, element.BindingConfiguration, evaluationContext, element);
                    StandardEndpointsSection.ValidateEndpointReference(element.Kind, element.EndpointConfiguration, evaluationContext, element);
                }
            }
        }
        public virtual bool CanAdd(TServiceModelExtensionElement element)
        {
            if (null == element)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("element");
            }

            bool retval      = false;
            Type elementType = element.GetType();

            if (!this.IsReadOnly())
            {
                if (!this.ContainsKey(elementType))
                {
                    retval = element.CanAdd(this.extensionCollectionName, ConfigurationHelpers.GetEvaluationContext(this));
                }
                else if (DiagnosticUtility.ShouldTraceWarning)
                {
                    TraceUtility.TraceEvent(TraceEventType.Warning,
                                            TraceCode.ExtensionElementAlreadyExistsInCollection,
                                            SR.GetString(SR.TraceCodeExtensionElementAlreadyExistsInCollection),
                                            this.CreateCanAddRecord(this[elementType]), this, null);
                }
            }
            else if (DiagnosticUtility.ShouldTraceWarning)
            {
                TraceUtility.TraceEvent(TraceEventType.Warning,
                                        TraceCode.ConfigurationIsReadOnly,
                                        SR.GetString(SR.TraceCodeConfigurationIsReadOnly),
                                        null, this, null);
            }
            return(retval);
        }
        private string GetBindingName()
        {
            string name = string.Empty;
            ExtensionElementCollection elements = null;

            System.Type type = base.GetType();
            elements = ExtensionsSection.UnsafeLookupCollection("bindingExtensions", ConfigurationHelpers.GetEvaluationContext(this));
            if (elements == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigExtensionCollectionNotFound", new object[] { "bindingExtensions" }), base.ElementInformation.Source, base.ElementInformation.LineNumber));
            }
            for (int i = 0; i < elements.Count; i++)
            {
                ExtensionElement element = elements[i];
                if (element.Type.Equals(type.AssemblyQualifiedName, StringComparison.Ordinal))
                {
                    name = element.Name;
                    break;
                }
                System.Type o = System.Type.GetType(element.Type, false);
                if ((null != o) && type.Equals(o))
                {
                    name = element.Name;
                    break;
                }
            }
            if (string.IsNullOrEmpty(name))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigExtensionTypeNotRegisteredInCollection", new object[] { type.AssemblyQualifiedName, "bindingExtensions" }), base.ElementInformation.Source, base.ElementInformation.LineNumber));
            }
            return(name);
        }
 internal ContextInformation GetOriginalContext(IConfigurationContextProviderInternal owner)
 {
     if (this.reset)
     {
         return(this.inheritedContext);
     }
     return(ConfigurationHelpers.GetEvaluationContext(owner));
 }
Beispiel #8
0
        internal void Validate()
        {
            ContextInformation evaluationContext = ConfigurationHelpers.GetEvaluationContext(this);

            if ((evaluationContext != null) && !string.IsNullOrEmpty(this.Binding))
            {
                BindingsSection.ValidateBindingReference(this.Binding, this.BindingConfiguration, evaluationContext, this);
            }
        }
 internal void AddItem(TServiceModelExtensionElement element)
 {
     if (this.IsReadOnly())
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigReadOnly)));
     }
     element.ExtensionCollectionName     = this.extensionCollectionName;
     element.ContainingEvaluationContext = ConfigurationHelpers.GetEvaluationContext(this);
     this.Items.Add(element);
     this.modified = true;
 }
        private string GetConfigurationElementName()
        {
            string name = string.Empty;
            ExtensionElementCollection elements = null;
            Type thisType = this.ThisType;
            ContextInformation containingEvaluationContext = this.ContainingEvaluationContext;

            if (containingEvaluationContext == null)
            {
                containingEvaluationContext = ConfigurationHelpers.GetEvaluationContext(this);
            }
            if (string.IsNullOrEmpty(this.extensionCollectionName))
            {
                if (DiagnosticUtility.ShouldTraceWarning)
                {
                    TraceUtility.TraceEvent(TraceEventType.Warning, 0x8001b, System.ServiceModel.SR.GetString("TraceCodeExtensionCollectionNameNotFound"), this, (Exception)null);
                }
                elements = ExtensionsSection.UnsafeLookupAssociatedCollection(this.ThisType, containingEvaluationContext, out this.extensionCollectionName);
            }
            else
            {
                elements = ExtensionsSection.UnsafeLookupCollection(this.extensionCollectionName, containingEvaluationContext);
            }
            if (elements == null)
            {
                if (string.IsNullOrEmpty(this.extensionCollectionName))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigNoExtensionCollectionAssociatedWithType", new object[] { thisType.AssemblyQualifiedName }), base.ElementInformation.Source, base.ElementInformation.LineNumber));
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigExtensionCollectionNotFound", new object[] { this.extensionCollectionName }), base.ElementInformation.Source, base.ElementInformation.LineNumber));
            }
            for (int i = 0; i < elements.Count; i++)
            {
                ExtensionElement element = elements[i];
                if (element.Type.Equals(thisType.AssemblyQualifiedName, StringComparison.Ordinal))
                {
                    name = element.Name;
                    break;
                }
                Type type = Type.GetType(element.Type, false);
                if ((null != type) && thisType.Equals(type))
                {
                    name = element.Name;
                    break;
                }
            }
            if (string.IsNullOrEmpty(name))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigExtensionTypeNotRegisteredInCollection", new object[] { thisType.AssemblyQualifiedName, this.extensionCollectionName }), base.ElementInformation.Source, base.ElementInformation.LineNumber));
            }
            return(name);
        }
 internal ContextInformation GetOriginalContext(IConfigurationContextProviderInternal owner)
 {
     if (this.reset)
     {
         // if reset, inherited context is authoritative, even if null
         return(this.inheritedContext);
     }
     else
     {
         // otherwise use current
         return(ConfigurationHelpers.GetEvaluationContext(owner));
     }
 }
        private TServiceModelExtensionElement CreateNewSection(string name)
        {
            if ((this.ContainsKey(name) && !(name == "clear")) && !(name == "remove"))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigDuplicateItem", new object[] { name, base.GetType().Name }), base.ElementInformation.Source, base.ElementInformation.LineNumber));
            }
            TServiceModelExtensionElement local = default(TServiceModelExtensionElement);
            Type extensionType = this.GetExtensionType(ConfigurationHelpers.GetEvaluationContext(this), name);

            if (null == extensionType)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigInvalidExtensionElementName", new object[] { name, this.extensionCollectionName }), base.ElementInformation.Source, base.ElementInformation.LineNumber));
            }
            if (!this.CollectionElementBaseType.IsAssignableFrom(extensionType))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigInvalidExtensionElement", new object[] { name, this.CollectionElementBaseType.FullName }), base.ElementInformation.Source, base.ElementInformation.LineNumber));
            }
            local = (TServiceModelExtensionElement)Activator.CreateInstance(extensionType);
            local.ExtensionCollectionName = this.extensionCollectionName;
            local.InternalInitializeDefault();
            return(local);
        }
Beispiel #13
0
 void UpdateEndpointSections()
 {
     UpdateEndpointSections(ConfigurationHelpers.GetEvaluationContext(this));
 }
        /// <summary>
        /// Returns the extension element, or null if the type cannot be loaded in certain situations (see the code for details).
        /// </summary>
        TServiceModelExtensionElement CreateNewSection(string name)
        {
            if (this.ContainsKey(name) && !(name == ConfigurationStrings.Clear || name == ConfigurationStrings.Remove))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigDuplicateItem,
                                                                                                                        name,
                                                                                                                        this.GetType().Name),
                                                                                                           this.ElementInformation.Source,
                                                                                                           this.ElementInformation.LineNumber));
            }

            TServiceModelExtensionElement retval = null;

            Type elementType;
            ContextInformation evaluationContext = ConfigurationHelpers.GetEvaluationContext(this);

            try
            {
                elementType = GetExtensionType(evaluationContext, name);
            }
            catch (ConfigurationErrorsException e)
            {
                // Work-around for bug 219506@CSDMain: if the extension type cannot be loaded, we'll ignore
                // the exception when running in win8 app container and reading from machine.config.
                if (System.ServiceModel.Channels.AppContainerInfo.IsRunningInAppContainer && evaluationContext.IsMachineLevel)
                {
                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                    return(null);
                }
                else
                {
                    throw;
                }
            }

            if (null != elementType)
            {
                if (this.CollectionElementBaseType.IsAssignableFrom(elementType))
                {
                    retval = (TServiceModelExtensionElement)Activator.CreateInstance(elementType);
                    retval.ExtensionCollectionName  = this.extensionCollectionName;
                    retval.ConfigurationElementName = name;
                    retval.InternalInitializeDefault();
                }
                else
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidExtensionElement,
                                                                                                                            name,
                                                                                                                            this.CollectionElementBaseType.FullName),
                                                                                                               this.ElementInformation.Source,
                                                                                                               this.ElementInformation.LineNumber));
                }
            }
            else
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidExtensionElementName,
                                                                                                                        name,
                                                                                                                        this.extensionCollectionName),
                                                                                                           this.ElementInformation.Source,
                                                                                                           this.ElementInformation.LineNumber));
            }

            return(retval);
        }
        string GetEndpointName()
        {
            string configuredSectionName          = String.Empty;
            ExtensionElementCollection collection = null;
            Type extensionSectionType             = this.GetType();

            collection = ExtensionsSection.UnsafeLookupCollection(ConfigurationStrings.EndpointExtensions, ConfigurationHelpers.GetEvaluationContext(this));

            if (null == collection)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigExtensionCollectionNotFound,
                                                                                                                        ConfigurationStrings.EndpointExtensions),
                                                                                                           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))
                {
                    configuredSectionName = 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))
                {
                    configuredSectionName = collectionElement.Name;
                    break;
                }
            }

            if (String.IsNullOrEmpty(configuredSectionName))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigExtensionTypeNotRegisteredInCollection,
                                                                                                                        extensionSectionType.AssemblyQualifiedName,
                                                                                                                        ConfigurationStrings.EndpointExtensions),
                                                                                                           this.ElementInformation.Source,
                                                                                                           this.ElementInformation.LineNumber));
            }

            return(configuredSectionName);
        }
        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 DESKTOP
                if (DiagnosticUtility.ShouldTraceWarning)
                {
                    TraceUtility.TraceEvent(TraceEventType.Warning,
                                            TraceCode.ExtensionCollectionNameNotFound,
                                            SR.GetString(SR.TraceCodeExtensionCollectionNameNotFound),
                                            this,
                                            (Exception)null);
                }
#endif

                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);
        }
 private void UpdateBindingSections()
 {
     this.UpdateBindingSections(ConfigurationHelpers.GetEvaluationContext(this));
 }