| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.ComponentModel; |
| | | 6 | | using System.Configuration; |
| | | 7 | | using CoreWCF.Channels; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Configuration |
| | | 10 | | { |
| | | 11 | | public sealed class CustomBindingElement : NamedServiceModelExtensionCollectionElement<BindingElementExtensionElemen |
| | | 12 | | { |
| | | 13 | | public CustomBindingElement() |
| | 40 | 14 | | : this(null) { } |
| | | 15 | | |
| | | 16 | | public CustomBindingElement(string name) : |
| | 40 | 17 | | base(ConfigurationStrings.BindingElementExtensions, name) { } |
| | | 18 | | |
| | | 19 | | [ConfigurationProperty(ConfigurationStrings.CloseTimeout, DefaultValue = ServiceDefaults.CloseTimeoutString)] |
| | | 20 | | [TypeConverter(typeof(TimeSpanOrInfiniteConverter))] |
| | | 21 | | public TimeSpan CloseTimeout |
| | | 22 | | { |
| | 20 | 23 | | get { return (TimeSpan)base[ConfigurationStrings.CloseTimeout]; } |
| | | 24 | | set |
| | | 25 | | { |
| | 0 | 26 | | base[ConfigurationStrings.CloseTimeout] = value; |
| | 0 | 27 | | SetIsModified(); |
| | 0 | 28 | | } |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | [ConfigurationProperty(ConfigurationStrings.OpenTimeout, DefaultValue = ServiceDefaults.OpenTimeoutString)] |
| | | 32 | | [TypeConverter(typeof(TimeSpanOrInfiniteConverter))] |
| | | 33 | | public TimeSpan OpenTimeout |
| | | 34 | | { |
| | 20 | 35 | | get { return (TimeSpan)base[ConfigurationStrings.OpenTimeout]; } |
| | | 36 | | set |
| | | 37 | | { |
| | 0 | 38 | | base[ConfigurationStrings.OpenTimeout] = value; |
| | 0 | 39 | | SetIsModified(); |
| | 0 | 40 | | } |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | [ConfigurationProperty(ConfigurationStrings.ReceiveTimeout, DefaultValue = ServiceDefaults.ReceiveTimeoutString) |
| | | 44 | | [TypeConverter(typeof(TimeSpanOrInfiniteConverter))] |
| | | 45 | | public TimeSpan ReceiveTimeout |
| | | 46 | | { |
| | 20 | 47 | | get { return (TimeSpan)base[ConfigurationStrings.ReceiveTimeout]; } |
| | | 48 | | set |
| | | 49 | | { |
| | 0 | 50 | | base[ConfigurationStrings.ReceiveTimeout] = value; |
| | 0 | 51 | | SetIsModified(); |
| | 0 | 52 | | } |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | [ConfigurationProperty(ConfigurationStrings.SendTimeout, DefaultValue = ServiceDefaults.SendTimeoutString)] |
| | | 56 | | [TypeConverter(typeof(TimeSpanOrInfiniteConverter))] |
| | | 57 | | public TimeSpan SendTimeout |
| | | 58 | | { |
| | 20 | 59 | | get { return (TimeSpan)base[ConfigurationStrings.SendTimeout]; } |
| | | 60 | | set |
| | | 61 | | { |
| | 0 | 62 | | base[ConfigurationStrings.SendTimeout] = value; |
| | 0 | 63 | | SetIsModified(); |
| | 0 | 64 | | } |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | public override void Add(BindingElementExtensionElement element) |
| | | 68 | | { |
| | 17 | 69 | | if (null == element) |
| | | 70 | | { |
| | 0 | 71 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element)); |
| | | 72 | | } |
| | | 73 | | |
| | 17 | 74 | | BindingElementExtensionElement existingElement = null; |
| | 17 | 75 | | if (!CanAddEncodingElement(element, ref existingElement)) |
| | | 76 | | { |
| | 0 | 77 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.Format(SR. |
| | 0 | 78 | | existingElement.ConfigurationElementName, |
| | 0 | 79 | | existingElement.GetType().AssemblyQualifiedName))); |
| | | 80 | | } |
| | 17 | 81 | | else if (!CanAddStreamUpgradeElement(element, ref existingElement)) |
| | | 82 | | { |
| | 0 | 83 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.Format(SR. |
| | 0 | 84 | | existingElement.ConfigurationElementName, |
| | 0 | 85 | | existingElement.GetType().AssemblyQualifiedName))); |
| | | 86 | | } |
| | 17 | 87 | | else if (!CanAddTransportElement(element, ref existingElement)) |
| | | 88 | | { |
| | 0 | 89 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.Format(SR. |
| | 0 | 90 | | existingElement.ConfigurationElementName, |
| | 0 | 91 | | existingElement.GetType().AssemblyQualifiedName))); |
| | | 92 | | } |
| | | 93 | | else |
| | | 94 | | { |
| | 17 | 95 | | base.Add(element); |
| | | 96 | | } |
| | 17 | 97 | | } |
| | | 98 | | |
| | | 99 | | public void ApplyConfiguration(Binding binding) |
| | | 100 | | { |
| | 20 | 101 | | if (null == binding) |
| | | 102 | | { |
| | 0 | 103 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(binding)); |
| | | 104 | | } |
| | 20 | 105 | | if (binding.GetType() != typeof(CustomBinding)) |
| | | 106 | | { |
| | 0 | 107 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.ConfigInvalidTypeForBinding, |
| | 0 | 108 | | typeof(CustomBinding).AssemblyQualifiedName, |
| | 0 | 109 | | binding.GetType().AssemblyQualifiedName)); |
| | | 110 | | } |
| | | 111 | | |
| | 20 | 112 | | binding.Name = Name; |
| | 20 | 113 | | binding.CloseTimeout = CloseTimeout; |
| | 20 | 114 | | binding.OpenTimeout = OpenTimeout; |
| | 20 | 115 | | binding.ReceiveTimeout = ReceiveTimeout; |
| | 20 | 116 | | binding.SendTimeout = SendTimeout; |
| | | 117 | | |
| | 20 | 118 | | OnApplyConfiguration(binding); |
| | 18 | 119 | | } |
| | | 120 | | |
| | | 121 | | public override bool CanAdd(BindingElementExtensionElement element) |
| | | 122 | | { |
| | 17 | 123 | | if (null == element) |
| | | 124 | | { |
| | 0 | 125 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element)); |
| | | 126 | | } |
| | | 127 | | |
| | 17 | 128 | | BindingElementExtensionElement existingElement = null; |
| | 17 | 129 | | return !ContainsKey(element.GetType()) && CanAddEncodingElement(element, ref existingElement) && |
| | 17 | 130 | | CanAddStreamUpgradeElement(element, ref existingElement) && CanAddTransportElement(element, ref existing |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | private bool CanAddEncodingElement(BindingElementExtensionElement element, ref BindingElementExtensionElement ex |
| | | 134 | | { |
| | 34 | 135 | | return CanAddExclusiveElement(typeof(MessageEncodingBindingElement), element.BindingElementType, ref existin |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | private bool CanAddExclusiveElement(Type exclusiveType, Type bindingElementType, ref BindingElementExtensionElem |
| | | 139 | | { |
| | 102 | 140 | | bool retval = true; |
| | 102 | 141 | | if (exclusiveType.IsAssignableFrom(bindingElementType)) |
| | | 142 | | { |
| | 56 | 143 | | foreach (BindingElementExtensionElement existing in this) |
| | | 144 | | { |
| | 0 | 145 | | if (exclusiveType.IsAssignableFrom(existing.BindingElementType)) |
| | | 146 | | { |
| | 0 | 147 | | retval = false; |
| | 0 | 148 | | existingElement = existing; |
| | 0 | 149 | | break; |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | } |
| | 102 | 153 | | return retval; |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | private bool CanAddStreamUpgradeElement(BindingElementExtensionElement element, ref BindingElementExtensionEleme |
| | | 157 | | { |
| | 34 | 158 | | return CanAddExclusiveElement(typeof(StreamUpgradeBindingElement), element.BindingElementType, ref existingE |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | private bool CanAddTransportElement(BindingElementExtensionElement element, ref BindingElementExtensionElement e |
| | | 162 | | { |
| | 34 | 163 | | return CanAddExclusiveElement(typeof(TransportBindingElement), element.BindingElementType, ref existingEleme |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | private void OnApplyConfiguration(Binding binding) |
| | | 167 | | { |
| | 20 | 168 | | CustomBinding theBinding = (CustomBinding)binding; |
| | 72 | 169 | | foreach (BindingElementExtensionElement bindingConfig in this) |
| | | 170 | | { |
| | 17 | 171 | | theBinding.Elements.Add(bindingConfig.CreateBindingElement()); |
| | | 172 | | } |
| | 18 | 173 | | } |
| | | 174 | | |
| | | 175 | | public Binding CreateBinding() |
| | | 176 | | { |
| | 20 | 177 | | CustomBinding customBinding = new CustomBinding(); |
| | 20 | 178 | | ApplyConfiguration(customBinding); |
| | 18 | 179 | | return customBinding; |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | //protected override object GetElementKey(ConfigurationElement element) => throw new NotImplementedException(); |
| | | 183 | | } |
| | | 184 | | } |