internal static BindingCollectionElement UnsafeGetAssociatedBindingCollectionElement(ContextInformation evaluationContext, string bindingCollectionName)
        {
            BindingCollectionElement retVal          = null;
            BindingsSection          bindingsSection = (BindingsSection)ConfigurationHelpers.UnsafeGetAssociatedSection(evaluationContext, ConfigurationStrings.BindingsSectionGroupPath);

            if (null != bindingsSection)
            {
                bindingsSection.UpdateBindingSections(evaluationContext);
                try
                {
                    retVal = bindingsSection[bindingCollectionName];
                }
                catch (KeyNotFoundException)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              new ConfigurationErrorsException(SR.GetString(SR.ConfigBindingExtensionNotFound,
                                                                            ConfigurationHelpers.GetBindingsSectionPath(bindingCollectionName))));
                }
                catch (NullReferenceException) // System.Configuration.ConfigurationElement
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              new ConfigurationErrorsException(SR.GetString(SR.ConfigBindingExtensionNotFound,
                                                                            ConfigurationHelpers.GetBindingsSectionPath(bindingCollectionName))));
                }
            }

            return(retVal);
        }
 internal static void ValidateBindingReference(string binding, string bindingConfiguration, ContextInformation evaluationContext, ConfigurationElement configurationElement)
 {
     if (evaluationContext == null)
     {
         DiagnosticUtility.FailFast("ValidateBindingReference() should only called with valid ContextInformation");
     }
     if (!string.IsNullOrEmpty(binding))
     {
         BindingCollectionElement element = null;
         if (evaluationContext != null)
         {
             element = ConfigurationHelpers.UnsafeGetAssociatedBindingCollectionElement(evaluationContext, binding);
         }
         else
         {
             element = ConfigurationHelpers.UnsafeGetBindingCollectionElement(binding);
         }
         if (element == null)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigInvalidSection", new object[] { ConfigurationHelpers.GetBindingsSectionPath(binding) }), configurationElement.ElementInformation.Source, configurationElement.ElementInformation.LineNumber));
         }
         if (!string.IsNullOrEmpty(bindingConfiguration) && !element.ContainsKey(bindingConfiguration))
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigInvalidBindingName", new object[] { bindingConfiguration, ConfigurationHelpers.GetBindingsSectionPath(binding), "bindingConfiguration" }), configurationElement.ElementInformation.Source, configurationElement.ElementInformation.LineNumber));
         }
     }
 }
        internal static bool TryAdd(string name, Binding binding, out string bindingSectionName)
        {
            // TryAdd built on assumption that BindingsSectionGroup.Configuration is valid.
            // This should be protected at the callers site.  If assumption is invalid, then
            // configuration system is in an indeterminate state.  Need to stop in a manner that
            // user code can not capture.
            if (null == BindingsSection.Configuration)
            {
                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.");
                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.");
            }

            bool            retval = false;
            string          outBindingSectionName = null;
            BindingsSection sectionGroup          = BindingsSection.GetSection(BindingsSection.Configuration);

            sectionGroup.UpdateBindingSections();
            foreach (string sectionName in sectionGroup.BindingCollectionElements.Keys)
            {
                BindingCollectionElement bindingCollectionElement = sectionGroup.BindingCollectionElements[sectionName];

                // Save the custom bindings as the last choice
                if (!(bindingCollectionElement is CustomBindingCollectionElement))
                {
                    MethodInfo tryAddMethod = bindingCollectionElement.GetType().GetMethod("TryAdd", BindingFlags.Instance | BindingFlags.NonPublic);
                    if (tryAddMethod != null)
                    {
                        retval = (bool)tryAddMethod.Invoke(bindingCollectionElement, new object[] { name, binding, BindingsSection.Configuration });
                        if (retval)
                        {
                            outBindingSectionName = sectionName;
                            break;
                        }
                    }
                }
            }
            if (!retval)
            {
                // Much of the time, the custombinding should come out ok.
                CustomBindingCollectionElement customBindingSection = CustomBindingCollectionElement.GetBindingCollectionElement();
                retval = customBindingSection.TryAdd(name, binding, BindingsSection.Configuration);
                if (retval)
                {
                    outBindingSectionName = ConfigurationStrings.CustomBindingCollectionElementName;
                }
            }

            // This little oddity exists to make sure that the out param is assigned to before the method
            // exits.
            bindingSectionName = outBindingSectionName;
            return(retval);
        }
        internal static void ValidateBindingReference(string binding, string bindingConfiguration, ContextInformation evaluationContext, ConfigurationElement configurationElement)
        {
            // ValidateBindingReference built on assumption that evaluationContext is valid.
            // This should be protected at the callers site.  If assumption is invalid, then
            // configuration system is in an indeterminate state.  Need to stop in a manner that
            // user code can not capture.
            if (null == evaluationContext)
            {
                Fx.Assert("ValidateBindingReference() should only called with valid ContextInformation");
                DiagnosticUtility.FailFast("ValidateBindingReference() should only called with valid ContextInformation");
            }

            if (!String.IsNullOrEmpty(binding))
            {
                BindingCollectionElement bindingCollectionElement = null;

                if (null != evaluationContext)
                {
                    bindingCollectionElement = ConfigurationHelpers.UnsafeGetAssociatedBindingCollectionElement(evaluationContext, binding);
                }
                else
                {
                    bindingCollectionElement = ConfigurationHelpers.UnsafeGetBindingCollectionElement(binding);
                }

                if (bindingCollectionElement == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidSection,
                                                                                                                            ConfigurationHelpers.GetBindingsSectionPath(binding)),
                                                                                                               configurationElement.ElementInformation.Source,
                                                                                                               configurationElement.ElementInformation.LineNumber));
                }

                if (!String.IsNullOrEmpty(bindingConfiguration))
                {
                    if (!bindingCollectionElement.ContainsKey(bindingConfiguration))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidBindingName,
                                                                                                                                bindingConfiguration,
                                                                                                                                ConfigurationHelpers.GetBindingsSectionPath(binding),
                                                                                                                                ConfigurationStrings.BindingConfiguration),
                                                                                                                   configurationElement.ElementInformation.Source,
                                                                                                                   configurationElement.ElementInformation.LineNumber));
                    }
                }
            }
        }
        public static Binding CreateBinding(string binding, string bindingConfiguration)
        {
            BindingCollectionElement section = ConfigUtil.BindingsSection [binding];

            if (section == null)
            {
                throw new ArgumentException(String.Format("binding section for {0} was not found.", binding));
            }

            Binding b = section.GetDefault();

            foreach (IBindingConfigurationElement el in section.ConfiguredBindings)
            {
                if (el.Name == bindingConfiguration)
                {
                    el.ApplyConfiguration(b);
                }
            }

            return(b);
        }
Example #6
0
        public static Binding CreateBinding(string binding, string bindingConfiguration)
        {
            BindingCollectionElement section = ConfigUtil.BindingsSection [binding];

            if (section == null)
            {
                throw new ArgumentException(String.Format("binding section for {0} was not found.", binding));
            }

            Binding b = (Binding)Activator.CreateInstance(section.BindingType, new object [0]);

            foreach (IBindingConfigurationElement el in section.ConfiguredBindings)
            {
                if (el.Name == bindingConfiguration)
                {
                    el.ApplyConfiguration(b);
                }
            }

            return(b);
        }
        internal static bool TryAdd(string name, Binding binding, out string bindingSectionName)
        {
            if (Configuration == null)
            {
                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.");
            }
            bool            flag    = false;
            string          str     = null;
            BindingsSection section = GetSection(Configuration);

            section.UpdateBindingSections();
            foreach (string str2 in section.BindingCollectionElements.Keys)
            {
                BindingCollectionElement element = section.BindingCollectionElements[str2];
                if (!(element is CustomBindingCollectionElement))
                {
                    MethodInfo method = element.GetType().GetMethod("TryAdd", BindingFlags.NonPublic | BindingFlags.Instance);
                    if (method != null)
                    {
                        flag = (bool)method.Invoke(element, new object[] { name, binding, Configuration });
                        if (flag)
                        {
                            str = str2;
                            break;
                        }
                    }
                }
            }
            if (!flag)
            {
                flag = CustomBindingCollectionElement.GetBindingCollectionElement().TryAdd(name, binding, Configuration);
                if (flag)
                {
                    str = "customBinding";
                }
            }
            bindingSectionName = str;
            return(flag);
        }
Example #8
0
        internal static BindingCollectionElement UnsafeGetAssociatedBindingCollectionElement(ContextInformation evaluationContext, string bindingCollectionName)
        {
            BindingCollectionElement element = null;
            BindingsSection          section = (BindingsSection)UnsafeGetAssociatedSection(evaluationContext, ConfigurationStrings.BindingsSectionGroupPath);

            if (section != null)
            {
                section.UpdateBindingSections(evaluationContext);
                try
                {
                    element = section[bindingCollectionName];
                }
                catch (KeyNotFoundException)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigBindingExtensionNotFound", new object[] { GetBindingsSectionPath(bindingCollectionName) })));
                }
                catch (NullReferenceException)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigBindingExtensionNotFound", new object[] { GetBindingsSectionPath(bindingCollectionName) })));
                }
            }
            return(element);
        }
Example #9
0
 private static Binding CreateBinding(IBindingConfigurationElement b, BindingCollectionElement be)
 {
     var binding = (Binding)Activator.CreateInstance(be.BindingType);
     b.ApplyConfiguration(binding);
     binding.Name = b.Name;
     return binding;
 }