| | | 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 CoreWCF.Configuration; |
| | | 6 | | using CoreWCF.Description; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Channels |
| | | 10 | | { |
| | | 11 | | public abstract class Binding : IDefaultCommunicationTimeouts |
| | | 12 | | { |
| | 11620 | 13 | | private TimeSpan _closeTimeout = ServiceDefaults.CloseTimeout; |
| | | 14 | | private string _name; |
| | | 15 | | private string _namespaceIdentifier; |
| | 11620 | 16 | | private TimeSpan _openTimeout = ServiceDefaults.OpenTimeout; |
| | 11620 | 17 | | private TimeSpan _receiveTimeout = ServiceDefaults.ReceiveTimeout; |
| | 11620 | 18 | | private TimeSpan _sendTimeout = ServiceDefaults.SendTimeout; |
| | | 19 | | internal const string DefaultNamespace = NamingHelper.DefaultNamespace; |
| | | 20 | | |
| | 11564 | 21 | | protected Binding() |
| | | 22 | | { |
| | 11564 | 23 | | _name = null; |
| | 11564 | 24 | | _namespaceIdentifier = DefaultNamespace; |
| | 11564 | 25 | | } |
| | | 26 | | |
| | 56 | 27 | | protected Binding(string name, string ns) |
| | | 28 | | { |
| | 56 | 29 | | if (string.IsNullOrEmpty(name)) |
| | | 30 | | { |
| | 0 | 31 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(name), SR.SFXBindingNameCannotBeNull |
| | | 32 | | } |
| | 56 | 33 | | if (ns == null) |
| | | 34 | | { |
| | 0 | 35 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(ns)); |
| | | 36 | | } |
| | | 37 | | |
| | 56 | 38 | | if (ns.Length > 0) |
| | | 39 | | { |
| | 56 | 40 | | NamingHelper.CheckUriParameter(ns, nameof(ns)); |
| | | 41 | | } |
| | | 42 | | |
| | 56 | 43 | | _name = name; |
| | 56 | 44 | | _namespaceIdentifier = ns; |
| | 56 | 45 | | } |
| | | 46 | | |
| | | 47 | | public TimeSpan CloseTimeout |
| | | 48 | | { |
| | 12994 | 49 | | get { return _closeTimeout; } |
| | | 50 | | set |
| | | 51 | | { |
| | 9183 | 52 | | if (value < TimeSpan.Zero) |
| | | 53 | | { |
| | 0 | 54 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 55 | | } |
| | 9183 | 56 | | if (TimeoutHelper.IsTooLarge(value)) |
| | | 57 | | { |
| | 0 | 58 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 59 | | } |
| | | 60 | | |
| | 9183 | 61 | | _closeTimeout = value; |
| | 9183 | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public string Name |
| | | 66 | | { |
| | | 67 | | get |
| | | 68 | | { |
| | 9913 | 69 | | if (_name == null) |
| | | 70 | | { |
| | 601 | 71 | | _name = GetType().Name; |
| | | 72 | | } |
| | | 73 | | |
| | 9913 | 74 | | return _name; |
| | | 75 | | } |
| | | 76 | | set |
| | | 77 | | { |
| | 9184 | 78 | | if (string.IsNullOrEmpty(value)) |
| | | 79 | | { |
| | 0 | 80 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(value), SR.SFXBindingNameCannotB |
| | | 81 | | } |
| | | 82 | | |
| | 9184 | 83 | | _name = value; |
| | 9184 | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | public string Namespace |
| | | 88 | | { |
| | 9847 | 89 | | get { return _namespaceIdentifier; } |
| | | 90 | | set |
| | | 91 | | { |
| | 9168 | 92 | | if (value == null) |
| | | 93 | | { |
| | 0 | 94 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 95 | | } |
| | | 96 | | |
| | 9168 | 97 | | if (value.Length > 0) |
| | | 98 | | { |
| | 9130 | 99 | | NamingHelper.CheckUriProperty(value, "Namespace"); |
| | | 100 | | } |
| | 9168 | 101 | | _namespaceIdentifier = value; |
| | 9168 | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | public TimeSpan OpenTimeout |
| | | 106 | | { |
| | 10338 | 107 | | get { return _openTimeout; } |
| | | 108 | | set |
| | | 109 | | { |
| | 9183 | 110 | | if (value < TimeSpan.Zero) |
| | | 111 | | { |
| | 0 | 112 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 113 | | } |
| | 9183 | 114 | | if (TimeoutHelper.IsTooLarge(value)) |
| | | 115 | | { |
| | 0 | 116 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 117 | | } |
| | | 118 | | |
| | 9183 | 119 | | _openTimeout = value; |
| | 9183 | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | public TimeSpan ReceiveTimeout |
| | | 124 | | { |
| | 10989 | 125 | | get { return _receiveTimeout; } |
| | | 126 | | set |
| | | 127 | | { |
| | 9209 | 128 | | if (value < TimeSpan.Zero) |
| | | 129 | | { |
| | 0 | 130 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 131 | | } |
| | 9209 | 132 | | if (TimeoutHelper.IsTooLarge(value)) |
| | | 133 | | { |
| | 0 | 134 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 135 | | } |
| | | 136 | | |
| | 9209 | 137 | | _receiveTimeout = value; |
| | 9209 | 138 | | } |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | public abstract string Scheme { get; } |
| | | 142 | | |
| | | 143 | | public MessageVersion MessageVersion |
| | | 144 | | { |
| | | 145 | | get |
| | | 146 | | { |
| | 4794 | 147 | | return GetProperty<MessageVersion>(new BindingParameterCollection()); |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | public TimeSpan SendTimeout |
| | | 152 | | { |
| | 11337 | 153 | | get { return _sendTimeout; } |
| | | 154 | | set |
| | | 155 | | { |
| | 9183 | 156 | | if (value < TimeSpan.Zero) |
| | | 157 | | { |
| | 0 | 158 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 159 | | } |
| | 9183 | 160 | | if (TimeoutHelper.IsTooLarge(value)) |
| | | 161 | | { |
| | 0 | 162 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 163 | | } |
| | | 164 | | |
| | 9183 | 165 | | _sendTimeout = value; |
| | 9183 | 166 | | } |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | private void ValidateSecurityCapabilities(ISecurityCapabilities runtimeSecurityCapabilities, BindingParameterCol |
| | | 170 | | { |
| | 0 | 171 | | ISecurityCapabilities bindingSecurityCapabilities = GetProperty<ISecurityCapabilities>(parameters); |
| | | 172 | | |
| | 0 | 173 | | if (!SecurityCapabilities.IsEqual(bindingSecurityCapabilities, runtimeSecurityCapabilities)) |
| | | 174 | | { |
| | 0 | 175 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 176 | | new InvalidOperationException(SR.Format(SR.SecurityCapabilitiesMismatched, this))); |
| | | 177 | | } |
| | 0 | 178 | | } |
| | | 179 | | |
| | | 180 | | public virtual bool CanBuildServiceDispatcher<TChannel>(BindingParameterCollection parameters) where TChannel : |
| | | 181 | | { |
| | 1850 | 182 | | BindingContext context = new BindingContext(new CustomBinding(this), parameters); |
| | 1850 | 183 | | return context.CanBuildNextServiceDispatcher<TChannel>(); |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | public virtual IServiceDispatcher BuildServiceDispatcher<TChannel>(BindingParameterCollection parameters, IServi |
| | | 187 | | where TChannel : class, IChannel |
| | | 188 | | { |
| | | 189 | | UriBuilder listenUriBuilder; |
| | 593 | 190 | | if (dispatcher.BaseAddress == null) |
| | | 191 | | { |
| | 0 | 192 | | listenUriBuilder = new UriBuilder(Scheme, DnsCache.MachineName); |
| | | 193 | | } |
| | | 194 | | else |
| | | 195 | | { |
| | 593 | 196 | | listenUriBuilder = new UriBuilder(dispatcher.BaseAddress); |
| | 593 | 197 | | listenUriBuilder.Scheme = Scheme; |
| | | 198 | | } |
| | | 199 | | |
| | 593 | 200 | | return BuildServiceDispatcher<TChannel>(listenUriBuilder.Uri, string.Empty, parameters, dispatcher); |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | public virtual IServiceDispatcher BuildServiceDispatcher<TChannel>(Uri listenUriBaseAddress, BindingParameterCol |
| | | 204 | | where TChannel : class, IChannel |
| | | 205 | | { |
| | 0 | 206 | | return BuildServiceDispatcher<TChannel>(listenUriBaseAddress, string.Empty, parameters, dispatcher); |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | public virtual IServiceDispatcher BuildServiceDispatcher<TChannel>(Uri listenUriBaseAddress, string listenUriRel |
| | | 210 | | where TChannel : class, IChannel |
| | | 211 | | { |
| | 593 | 212 | | EnsureInvariants(); |
| | 593 | 213 | | if (!(this is CustomBinding binding)) |
| | | 214 | | { |
| | 0 | 215 | | binding = new CustomBinding(this); |
| | | 216 | | } |
| | | 217 | | |
| | 593 | 218 | | BindingContext context = new BindingContext(binding, parameters, listenUriBaseAddress, listenUriRelativeAddr |
| | 593 | 219 | | IServiceDispatcher serviceDispatcher = context.BuildNextServiceDispatcher<TChannel>(dispatcher); |
| | 593 | 220 | | context.ValidateBindingElementsConsumed(); |
| | | 221 | | |
| | | 222 | | // TODO: Work out how to validate security capabilities |
| | | 223 | | //this.ValidateSecurityCapabilities(serviceDispatcher.GetProperty<ISecurityCapabilities>(), parameters); |
| | | 224 | | |
| | 593 | 225 | | return serviceDispatcher; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | public abstract BindingElementCollection CreateBindingElements(); |
| | | 229 | | |
| | | 230 | | public T GetProperty<T>(BindingParameterCollection parameters) where T : class |
| | | 231 | | { |
| | 6036 | 232 | | BindingContext context = new BindingContext(new CustomBinding(this), parameters); |
| | 6036 | 233 | | return context.GetInnerProperty<T>(); |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | private void EnsureInvariants() |
| | | 237 | | { |
| | 593 | 238 | | EnsureInvariants(null); |
| | 593 | 239 | | } |
| | | 240 | | |
| | | 241 | | internal void EnsureInvariants(string contractName) |
| | | 242 | | { |
| | 1234 | 243 | | BindingElementCollection elements = CreateBindingElements(); |
| | 1234 | 244 | | TransportBindingElement transport = null; |
| | | 245 | | int index; |
| | 5044 | 246 | | for (index = 0; index < elements.Count; index++) |
| | | 247 | | { |
| | 2522 | 248 | | transport = elements[index] as TransportBindingElement; |
| | 2522 | 249 | | if (transport != null) |
| | | 250 | | { |
| | | 251 | | break; |
| | | 252 | | } |
| | | 253 | | } |
| | | 254 | | |
| | 1234 | 255 | | if (transport == null) |
| | | 256 | | { |
| | 0 | 257 | | if (contractName == null) |
| | | 258 | | { |
| | 0 | 259 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 260 | | SR.Format(SR.CustomBindingRequiresTransport, Name))); |
| | | 261 | | } |
| | | 262 | | else |
| | | 263 | | { |
| | 0 | 264 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 265 | | SR.Format(SR.SFxCustomBindingNeedsTransport1, contractName))); |
| | | 266 | | } |
| | | 267 | | } |
| | 1234 | 268 | | if (index != elements.Count - 1) |
| | | 269 | | { |
| | 0 | 270 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 271 | | SR.Format(SR.TransportBindingElementMustBeLast, Name, transport.GetType().Name))); |
| | | 272 | | } |
| | 1234 | 273 | | if (string.IsNullOrEmpty(transport.Scheme)) |
| | | 274 | | { |
| | 0 | 275 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 276 | | SR.Format(SR.InvalidBindingScheme, transport.GetType().Name))); |
| | | 277 | | } |
| | | 278 | | |
| | 1234 | 279 | | if (MessageVersion == null) |
| | | 280 | | { |
| | 0 | 281 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 282 | | SR.Format(SR.MessageVersionMissingFromBinding, Name))); |
| | | 283 | | } |
| | 1234 | 284 | | } |
| | | 285 | | } |
| | | 286 | | } |