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 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);
        }
Пример #3
0
        static BindingCollectionElement CreateCollectionElement(
			string bindingName, IBindingConfigurationElement element)
        {
            switch (bindingName) {
            case "basicHttpBinding": {
                var http = new BasicHttpBindingCollectionElement ();
                http.Bindings.Add ((BasicHttpBindingElement)element);
                return http;
            }
            #if NET_4_5
            case "basicHttpsBinding": {
                var https = new BasicHttpsBindingCollectionElement ();
                https.Bindings.Add ((BasicHttpsBindingElement)element);
                return https;
            }
            #endif
            case "netTcpBinding": {
                var netTcp = new NetTcpBindingCollectionElement ();
                netTcp.Bindings.Add ((NetTcpBindingElement)element);
                return netTcp;
            }
            case "customBinding": {
                var custom = new CustomBindingCollectionElement ();
                custom.Bindings.Add ((CustomBindingElement)element);
                return custom;
            }
            default:
                throw new InvalidOperationException ();
            }
        }