| | | 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.Collections.Generic; |
| | | 6 | | using System.ComponentModel; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.Description; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Channels |
| | | 12 | | { |
| | | 13 | | public abstract class ConnectionOrientedTransportBindingElement : TransportBindingElement, IWsdlExportExtension, IPo |
| | | 14 | | { |
| | | 15 | | private int _connectionBufferSize; |
| | | 16 | | private readonly bool _exposeConnectionProperty; |
| | | 17 | | private HostNameComparisonMode _hostNameComparisonMode; |
| | | 18 | | private TimeSpan _channelInitializationTimeout; |
| | | 19 | | private int _maxBufferSize; |
| | | 20 | | private bool _maxBufferSizeInitialized; |
| | | 21 | | private int _maxPendingConnections; |
| | | 22 | | private TimeSpan _maxOutputDelay; |
| | | 23 | | private int _maxPendingAccepts; |
| | | 24 | | private TransferMode _transferMode; |
| | | 25 | | |
| | 127 | 26 | | protected ConnectionOrientedTransportBindingElement() : base() |
| | | 27 | | { |
| | 127 | 28 | | _connectionBufferSize = ConnectionOrientedTransportDefaults.ConnectionBufferSize; |
| | 127 | 29 | | _hostNameComparisonMode = ConnectionOrientedTransportDefaults.HostNameComparisonMode; |
| | 127 | 30 | | _channelInitializationTimeout = ConnectionOrientedTransportDefaults.ChannelInitializationTimeout; |
| | 127 | 31 | | _maxBufferSize = TransportDefaults.MaxBufferSize; |
| | 127 | 32 | | _maxPendingConnections = ConnectionOrientedTransportDefaults.GetMaxPendingConnections(); |
| | 127 | 33 | | _maxOutputDelay = ConnectionOrientedTransportDefaults.MaxOutputDelay; |
| | 127 | 34 | | _maxPendingAccepts = ConnectionOrientedTransportDefaults.GetMaxPendingAccepts(); |
| | 127 | 35 | | _transferMode = ConnectionOrientedTransportDefaults.TransferMode; |
| | 127 | 36 | | } |
| | | 37 | | |
| | 1905 | 38 | | protected ConnectionOrientedTransportBindingElement(ConnectionOrientedTransportBindingElement elementToBeCloned) |
| | | 39 | | { |
| | 1905 | 40 | | _connectionBufferSize = elementToBeCloned._connectionBufferSize; |
| | 1905 | 41 | | _exposeConnectionProperty = elementToBeCloned._exposeConnectionProperty; |
| | 1905 | 42 | | _hostNameComparisonMode = elementToBeCloned._hostNameComparisonMode; |
| | 1905 | 43 | | InheritBaseAddressSettings = elementToBeCloned.InheritBaseAddressSettings; |
| | 1905 | 44 | | _channelInitializationTimeout = elementToBeCloned.ChannelInitializationTimeout; |
| | 1905 | 45 | | _maxBufferSize = elementToBeCloned._maxBufferSize; |
| | 1905 | 46 | | _maxBufferSizeInitialized = elementToBeCloned._maxBufferSizeInitialized; |
| | 1905 | 47 | | _maxPendingConnections = elementToBeCloned._maxPendingConnections; |
| | 1905 | 48 | | _maxOutputDelay = elementToBeCloned._maxOutputDelay; |
| | 1905 | 49 | | _maxPendingAccepts = elementToBeCloned._maxPendingAccepts; |
| | 1905 | 50 | | _transferMode = elementToBeCloned._transferMode; |
| | 1905 | 51 | | IsMaxPendingAcceptsSet = elementToBeCloned.IsMaxPendingAcceptsSet; |
| | 1905 | 52 | | } |
| | | 53 | | |
| | | 54 | | [DefaultValue(ConnectionOrientedTransportDefaults.ConnectionBufferSize)] |
| | | 55 | | public int ConnectionBufferSize |
| | | 56 | | { |
| | | 57 | | get |
| | | 58 | | { |
| | 110 | 59 | | return _connectionBufferSize; |
| | | 60 | | } |
| | | 61 | | set |
| | | 62 | | { |
| | 2 | 63 | | if (value < 0) |
| | | 64 | | { |
| | 0 | 65 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 66 | | SRCommon.ValueMustBeNonNegative)); |
| | | 67 | | } |
| | | 68 | | |
| | 2 | 69 | | _connectionBufferSize = value; |
| | 2 | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | [DefaultValue(ConnectionOrientedTransportDefaults.HostNameComparisonMode)] |
| | | 74 | | public HostNameComparisonMode HostNameComparisonMode |
| | | 75 | | { |
| | | 76 | | get |
| | | 77 | | { |
| | 110 | 78 | | return _hostNameComparisonMode; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | set |
| | | 82 | | { |
| | 11 | 83 | | HostNameComparisonModeHelper.Validate(value); |
| | 11 | 84 | | _hostNameComparisonMode = value; |
| | 11 | 85 | | } |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | [DefaultValue(TransportDefaults.MaxBufferSize)] |
| | | 89 | | public int MaxBufferSize |
| | | 90 | | { |
| | | 91 | | get |
| | | 92 | | { |
| | 112 | 93 | | if (_maxBufferSizeInitialized || TransferMode != TransferMode.Buffered) |
| | | 94 | | { |
| | 39 | 95 | | return _maxBufferSize; |
| | | 96 | | } |
| | | 97 | | |
| | 73 | 98 | | long maxReceivedMessageSize = MaxReceivedMessageSize; |
| | 73 | 99 | | if (maxReceivedMessageSize > int.MaxValue) |
| | | 100 | | { |
| | 0 | 101 | | return int.MaxValue; |
| | | 102 | | } |
| | | 103 | | else |
| | | 104 | | { |
| | 73 | 105 | | return (int)maxReceivedMessageSize; |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | set |
| | | 109 | | { |
| | 10 | 110 | | if (value <= 0) |
| | | 111 | | { |
| | 0 | 112 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 113 | | SRCommon.ValueMustBePositive)); |
| | | 114 | | } |
| | | 115 | | |
| | 10 | 116 | | _maxBufferSizeInitialized = true; |
| | 10 | 117 | | _maxBufferSize = value; |
| | 10 | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | public int MaxPendingConnections |
| | | 122 | | { |
| | | 123 | | get |
| | | 124 | | { |
| | 1 | 125 | | return _maxPendingConnections; |
| | | 126 | | } |
| | | 127 | | set |
| | | 128 | | { |
| | 10 | 129 | | if (value <= 0) |
| | | 130 | | { |
| | 0 | 131 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 132 | | SRCommon.ValueMustBePositive)); |
| | | 133 | | } |
| | | 134 | | |
| | 10 | 135 | | _maxPendingConnections = value; |
| | 10 | 136 | | } |
| | | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | internal bool IsMaxPendingConnectionsSet { get; private set; } |
| | | 140 | | |
| | | 141 | | // used by MEX to ensure that we don't conflict on base-address scoped settings |
| | 3810 | 142 | | internal bool InheritBaseAddressSettings { get; set; } |
| | | 143 | | |
| | | 144 | | public TimeSpan ChannelInitializationTimeout |
| | | 145 | | { |
| | | 146 | | get |
| | | 147 | | { |
| | 1907 | 148 | | return _channelInitializationTimeout; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | set |
| | | 152 | | { |
| | 2 | 153 | | if (value <= TimeSpan.Zero) |
| | | 154 | | { |
| | 0 | 155 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 156 | | SRCommon.TimeSpanMustBeGreaterThanTimeSpanZero)); |
| | | 157 | | } |
| | | 158 | | |
| | 2 | 159 | | if (TimeoutHelper.IsTooLarge(value)) |
| | | 160 | | { |
| | 0 | 161 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 162 | | SRCommon.SFxTimeoutOutOfRangeTooBig)); |
| | | 163 | | } |
| | | 164 | | |
| | 2 | 165 | | _channelInitializationTimeout = value; |
| | 2 | 166 | | } |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | public TimeSpan MaxOutputDelay |
| | | 170 | | { |
| | | 171 | | get |
| | | 172 | | { |
| | 2 | 173 | | return _maxOutputDelay; |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | set |
| | | 177 | | { |
| | 2 | 178 | | if (value < TimeSpan.Zero) |
| | | 179 | | { |
| | 0 | 180 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 181 | | SRCommon.SFxTimeoutOutOfRange0)); |
| | | 182 | | } |
| | | 183 | | |
| | 2 | 184 | | if (TimeoutHelper.IsTooLarge(value)) |
| | | 185 | | { |
| | 0 | 186 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 187 | | SRCommon.SFxTimeoutOutOfRangeTooBig)); |
| | | 188 | | } |
| | | 189 | | |
| | 2 | 190 | | _maxOutputDelay = value; |
| | 2 | 191 | | } |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | public int MaxPendingAccepts |
| | | 195 | | { |
| | | 196 | | get |
| | | 197 | | { |
| | 1 | 198 | | return _maxPendingAccepts; |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | set |
| | | 202 | | { |
| | 1 | 203 | | if (value <= 0) |
| | | 204 | | { |
| | 0 | 205 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 206 | | SRCommon.ValueMustBePositive)); |
| | | 207 | | } |
| | | 208 | | |
| | 1 | 209 | | _maxPendingAccepts = value; |
| | 1 | 210 | | IsMaxPendingAcceptsSet = true; |
| | 1 | 211 | | } |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | public override bool CanBuildServiceDispatcher<TChannel>(BindingContext context) |
| | | 215 | | { |
| | 117 | 216 | | if (context == null) |
| | | 217 | | { |
| | 0 | 218 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context)); |
| | | 219 | | } |
| | | 220 | | |
| | 117 | 221 | | return true; |
| | | 222 | | } |
| | | 223 | | |
| | 3811 | 224 | | internal bool IsMaxPendingAcceptsSet { get; private set; } |
| | | 225 | | |
| | | 226 | | [DefaultValue(ConnectionOrientedTransportDefaults.TransferMode)] |
| | | 227 | | public TransferMode TransferMode |
| | | 228 | | { |
| | | 229 | | get |
| | | 230 | | { |
| | 221 | 231 | | return _transferMode; |
| | | 232 | | } |
| | | 233 | | set |
| | | 234 | | { |
| | 51 | 235 | | TransferModeHelper.Validate(value); |
| | 51 | 236 | | _transferMode = value; |
| | 51 | 237 | | } |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | void IPolicyExportExtension.ExportPolicy(MetadataExporter exporter, PolicyConversionContext context) |
| | | 241 | | { |
| | 1 | 242 | | if (exporter == null) |
| | | 243 | | { |
| | 0 | 244 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(exporter)); |
| | | 245 | | } |
| | | 246 | | |
| | 1 | 247 | | if (context == null) |
| | | 248 | | { |
| | 0 | 249 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context)); |
| | | 250 | | } |
| | | 251 | | |
| | 1 | 252 | | ICollection<XmlElement> policyAssertions = context.GetBindingAssertions(); |
| | 1 | 253 | | if (TransferModeHelper.IsRequestStreamed(TransferMode) |
| | 1 | 254 | | || TransferModeHelper.IsResponseStreamed(TransferMode)) |
| | | 255 | | { |
| | 0 | 256 | | policyAssertions.Add(new XmlDocument().CreateElement(TransportPolicyConstants.DotNetFramingPrefix, |
| | 0 | 257 | | TransportPolicyConstants.StreamedName, TransportPolicyConstants.DotNetFramingNamespace)); |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | bool createdNew; |
| | 1 | 261 | | MessageEncodingBindingElement encodingBindingElement = FindMessageEncodingBindingElement(context.BindingElem |
| | 1 | 262 | | if (createdNew && encodingBindingElement is IPolicyExportExtension) |
| | | 263 | | { |
| | 0 | 264 | | encodingBindingElement = new BinaryMessageEncodingBindingElement(); |
| | 0 | 265 | | ((IPolicyExportExtension)encodingBindingElement).ExportPolicy(exporter, context); |
| | | 266 | | } |
| | | 267 | | |
| | 1 | 268 | | WsdlExporter.AddWSAddressingAssertion(exporter, context, encodingBindingElement.MessageVersion.Addressing); |
| | 1 | 269 | | } |
| | | 270 | | |
| | 0 | 271 | | void IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext context) { } |
| | | 272 | | |
| | | 273 | | protected abstract string WsdlTransportUri { get; } |
| | | 274 | | |
| | | 275 | | void IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext endpointContext) |
| | | 276 | | { |
| | | 277 | | bool createdNew; |
| | 1 | 278 | | MessageEncodingBindingElement encodingBindingElement = FindMessageEncodingBindingElement(endpointContext, ou |
| | 1 | 279 | | ExportWsdlEndpoint(exporter, endpointContext, WsdlTransportUri, encodingBindingElement.MessageVersion.Addres |
| | 1 | 280 | | } |
| | | 281 | | |
| | | 282 | | private MessageEncodingBindingElement FindMessageEncodingBindingElement(BindingElementCollection bindingElements |
| | | 283 | | { |
| | 2 | 284 | | createdNew = false; |
| | 2 | 285 | | MessageEncodingBindingElement encodingBindingElement = bindingElements.Find<MessageEncodingBindingElement>() |
| | 2 | 286 | | if (encodingBindingElement == null) |
| | | 287 | | { |
| | 0 | 288 | | createdNew = true; |
| | 0 | 289 | | encodingBindingElement = new BinaryMessageEncodingBindingElement(); |
| | | 290 | | } |
| | 2 | 291 | | return encodingBindingElement; |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | private MessageEncodingBindingElement FindMessageEncodingBindingElement(WsdlEndpointConversionContext endpointCo |
| | | 295 | | { |
| | 1 | 296 | | BindingElementCollection bindingElements = endpointContext.Endpoint.Binding.CreateBindingElements(); |
| | 1 | 297 | | return FindMessageEncodingBindingElement(bindingElements, out createdNew); |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | public override T GetProperty<T>(BindingContext context) |
| | | 301 | | { |
| | 330 | 302 | | if (context == null) |
| | | 303 | | { |
| | 0 | 304 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context)); |
| | | 305 | | } |
| | 330 | 306 | | if (typeof(T) == typeof(TransferMode)) |
| | | 307 | | { |
| | 0 | 308 | | return (T)(object)TransferMode; |
| | | 309 | | } |
| | | 310 | | else |
| | | 311 | | { |
| | 330 | 312 | | return base.GetProperty<T>(context); |
| | | 313 | | } |
| | | 314 | | } |
| | | 315 | | |
| | | 316 | | protected override bool IsMatch(BindingElement b) |
| | | 317 | | { |
| | 0 | 318 | | if (!base.IsMatch(b)) |
| | | 319 | | { |
| | 0 | 320 | | return false; |
| | | 321 | | } |
| | | 322 | | |
| | 0 | 323 | | if (!(b is ConnectionOrientedTransportBindingElement connection)) |
| | | 324 | | { |
| | 0 | 325 | | return false; |
| | | 326 | | } |
| | | 327 | | |
| | 0 | 328 | | if (_connectionBufferSize != connection._connectionBufferSize) |
| | | 329 | | { |
| | 0 | 330 | | return false; |
| | | 331 | | } |
| | | 332 | | |
| | 0 | 333 | | if (_hostNameComparisonMode != connection._hostNameComparisonMode) |
| | | 334 | | { |
| | 0 | 335 | | return false; |
| | | 336 | | } |
| | | 337 | | |
| | 0 | 338 | | if (InheritBaseAddressSettings != connection.InheritBaseAddressSettings) |
| | | 339 | | { |
| | 0 | 340 | | return false; |
| | | 341 | | } |
| | | 342 | | |
| | 0 | 343 | | if (_channelInitializationTimeout != connection._channelInitializationTimeout) |
| | | 344 | | { |
| | 0 | 345 | | return false; |
| | | 346 | | } |
| | 0 | 347 | | if (_maxBufferSize != connection._maxBufferSize) |
| | | 348 | | { |
| | 0 | 349 | | return false; |
| | | 350 | | } |
| | | 351 | | |
| | 0 | 352 | | if (_maxPendingConnections != connection._maxPendingConnections) |
| | | 353 | | { |
| | 0 | 354 | | return false; |
| | | 355 | | } |
| | | 356 | | |
| | 0 | 357 | | if (_maxOutputDelay != connection._maxOutputDelay) |
| | | 358 | | { |
| | 0 | 359 | | return false; |
| | | 360 | | } |
| | | 361 | | |
| | 0 | 362 | | if (_maxPendingAccepts != connection._maxPendingAccepts) |
| | | 363 | | { |
| | 0 | 364 | | return false; |
| | | 365 | | } |
| | | 366 | | |
| | 0 | 367 | | if (_transferMode != connection._transferMode) |
| | | 368 | | { |
| | 0 | 369 | | return false; |
| | | 370 | | } |
| | | 371 | | |
| | 0 | 372 | | return true; |
| | | 373 | | } |
| | | 374 | | |
| | | 375 | | // Originally lived on ConnectionOrientedTransportChannelListener in WCF |
| | 19 | 376 | | internal protected virtual bool SupportsUpgrade(StreamUpgradeBindingElement streamUpgradeBindingElement) => true |
| | | 377 | | |
| | | 378 | | internal static void ExportWsdlEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext endpointContext, |
| | | 379 | | string wsdlTransportUri, AddressingVersion addressingVersion) |
| | | 380 | | { |
| | 1 | 381 | | ExportWsdlEndpoint(exporter, endpointContext, wsdlTransportUri, endpointContext.Endpoint.Address, addressing |
| | 1 | 382 | | } |
| | | 383 | | } |
| | | 384 | | |
| | | 385 | | // Originally lived in TransportBindingElementImporter.cs |
| | | 386 | | internal static class TransportPolicyConstants |
| | | 387 | | { |
| | | 388 | | public const string BasicHttpAuthenticationName = "BasicAuthentication"; |
| | | 389 | | public const string CompositeDuplex = "CompositeDuplex"; |
| | | 390 | | public const string CompositeDuplexNamespace = "http://schemas.microsoft.com/net/2006/06/duplex"; |
| | | 391 | | public const string CompositeDuplexPrefix = "cdp"; |
| | | 392 | | public const string DigestHttpAuthenticationName = "DigestAuthentication"; |
| | | 393 | | public const string DotNetFramingNamespace = Framing.FramingEncodingString.NamespaceUri + "/policy"; |
| | | 394 | | public const string DotNetFramingPrefix = "msf"; |
| | | 395 | | public const string HttpTransportNamespace = "http://schemas.microsoft.com/ws/06/2004/policy/http"; |
| | | 396 | | public const string HttpTransportPrefix = "http"; |
| | | 397 | | public const string HttpTransportUri = "http://schemas.xmlsoap.org/soap/http"; |
| | | 398 | | public const string MsmqBestEffort = "MsmqBestEffort"; |
| | | 399 | | public const string MsmqSession = "MsmqSession"; |
| | | 400 | | public const string MsmqTransportNamespace = "http://schemas.microsoft.com/ws/06/2004/mspolicy/msmq"; |
| | | 401 | | public const string MsmqTransportPrefix = "msmq"; |
| | | 402 | | public const string MsmqTransportUri = "http://schemas.microsoft.com/soap/msmq"; |
| | | 403 | | public const string MsmqVolatile = "MsmqVolatile"; |
| | | 404 | | public const string MsmqAuthenticated = "Authenticated"; |
| | | 405 | | public const string MsmqWindowsDomain = "WindowsDomain"; |
| | | 406 | | public const string NamedPipeTransportUri = "http://schemas.microsoft.com/soap/named-pipe"; |
| | | 407 | | public const string NegotiateHttpAuthenticationName = "NegotiateAuthentication"; |
| | | 408 | | public const string NtlmHttpAuthenticationName = "NtlmAuthentication"; |
| | | 409 | | public const string PeerTransportUri = "http://schemas.microsoft.com/soap/peer"; |
| | | 410 | | public const string ProtectionLevelName = "ProtectionLevel"; |
| | | 411 | | public const string RequireClientCertificateName = "RequireClientCertificate"; |
| | | 412 | | public const string SslTransportSecurityName = "SslTransportSecurity"; |
| | | 413 | | public const string StreamedName = "Streamed"; |
| | | 414 | | public const string TcpTransportUri = "http://schemas.microsoft.com/soap/tcp"; |
| | | 415 | | public const string WebSocketPolicyPrefix = "mswsp"; |
| | | 416 | | public const string WebSocketPolicyNamespace = "http://schemas.microsoft.com/soap/websocket/policy"; |
| | | 417 | | public const string WebSocketTransportUri = "http://schemas.microsoft.com/soap/websocket"; |
| | | 418 | | public const string WebSocketEnabled = "WebSocketEnabled"; |
| | | 419 | | public const string WindowsTransportSecurityName = "WindowsTransportSecurity"; |
| | | 420 | | } |
| | | 421 | | } |