1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //------------------------------------------------------------
5 namespace System.ServiceModel.Channels
7 using System.Collections.Generic;
8 using System.ServiceModel.Description;
9 using System.Runtime.Serialization;
10 using System.ServiceModel.Security;
11 using System.ServiceModel;
13 using WsdlNS = System.Web.Services.Description;
14 using System.Collections.ObjectModel;
15 using System.ComponentModel;
17 public abstract class TransportBindingElement
20 bool manualAddressing;
21 long maxBufferPoolSize;
22 long maxReceivedMessageSize;
24 protected TransportBindingElement()
26 this.manualAddressing = TransportDefaults.ManualAddressing;
27 this.maxBufferPoolSize = TransportDefaults.MaxBufferPoolSize;
28 this.maxReceivedMessageSize = TransportDefaults.MaxReceivedMessageSize;
31 protected TransportBindingElement(TransportBindingElement elementToBeCloned)
32 : base(elementToBeCloned)
34 this.manualAddressing = elementToBeCloned.manualAddressing;
35 this.maxBufferPoolSize = elementToBeCloned.maxBufferPoolSize;
36 this.maxReceivedMessageSize = elementToBeCloned.maxReceivedMessageSize;
39 [DefaultValue(TransportDefaults.ManualAddressing)]
40 public virtual bool ManualAddressing
44 return this.manualAddressing;
49 this.manualAddressing = value;
53 [DefaultValue(TransportDefaults.MaxBufferPoolSize)]
54 public virtual long MaxBufferPoolSize
58 return this.maxBufferPoolSize;
64 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
65 SR.GetString(SR.ValueMustBeNonNegative)));
67 this.maxBufferPoolSize = value;
71 [DefaultValue(TransportDefaults.MaxReceivedMessageSize)]
72 public virtual long MaxReceivedMessageSize
76 return this.maxReceivedMessageSize;
82 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
83 SR.GetString(SR.ValueMustBePositive)));
85 this.maxReceivedMessageSize = value;
89 public abstract string Scheme { get; }
91 internal static IChannelFactory<TChannel> CreateChannelFactory<TChannel>(TransportBindingElement transport)
93 Binding binding = new CustomBinding(transport);
94 return binding.BuildChannelFactory<TChannel>();
97 internal static IChannelListener CreateChannelListener<TChannel>(TransportBindingElement transport)
98 where TChannel : class, IChannel
100 Binding binding = new CustomBinding(transport);
101 return binding.BuildChannelListener<TChannel>();
104 public override T GetProperty<T>(BindingContext context)
108 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
111 if (typeof(T) == typeof(ChannelProtectionRequirements))
113 ChannelProtectionRequirements myRequirements = this.GetProtectionRequirements(context);
114 myRequirements.Add(context.GetInnerProperty<ChannelProtectionRequirements>() ?? new ChannelProtectionRequirements());
115 return (T)(object)myRequirements;
118 // to cover all our bases, let's iterate through the BindingParameters to make sure
119 // we haven't missed a query (since we're the Transport and we're at the bottom)
120 #pragma warning suppress 56506 // Microsoft, BindingContext.BindingParameters cannot be null
121 Collection<BindingElement> bindingElements = context.BindingParameters.FindAll<BindingElement>();
123 T result = default(T);
124 for (int i = 0; i < bindingElements.Count; i++)
126 result = bindingElements[i].GetIndividualProperty<T>();
127 if (result != default(T))
133 if (typeof(T) == typeof(MessageVersion))
135 return (T)(object)TransportDefaults.GetDefaultMessageEncoderFactory().MessageVersion;
138 if (typeof(T) == typeof(XmlDictionaryReaderQuotas))
140 return (T)(object)new XmlDictionaryReaderQuotas();
146 ChannelProtectionRequirements GetProtectionRequirements(AddressingVersion addressingVersion)
148 ChannelProtectionRequirements result = new ChannelProtectionRequirements();
149 result.IncomingSignatureParts.AddParts(addressingVersion.SignedMessageParts);
150 result.OutgoingSignatureParts.AddParts(addressingVersion.SignedMessageParts);
154 internal ChannelProtectionRequirements GetProtectionRequirements(BindingContext context)
156 AddressingVersion addressingVersion = AddressingVersion.WSAddressing10;
157 #pragma warning suppress 56506 // Microsoft, CustomBinding.Elements can never be null
158 MessageEncodingBindingElement messageEncoderBindingElement = context.Binding.Elements.Find<MessageEncodingBindingElement>();
159 if (messageEncoderBindingElement != null)
161 addressingVersion = messageEncoderBindingElement.MessageVersion.Addressing;
163 return GetProtectionRequirements(addressingVersion);
166 internal static void ExportWsdlEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext endpointContext,
167 string wsdlTransportUri, AddressingVersion addressingVersion)
169 ExportWsdlEndpoint(exporter, endpointContext, wsdlTransportUri, endpointContext.Endpoint.Address, addressingVersion);
172 internal static void ExportWsdlEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext endpointContext,
173 string wsdlTransportUri, EndpointAddress address, AddressingVersion addressingVersion)
175 if (exporter == null)
177 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
180 if (endpointContext == null)
182 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("endpointContext");
185 // Set SoapBinding Transport URI
186 #pragma warning suppress 56506 // Microsoft, these properties cannot be null in this context
187 BindingElementCollection bindingElements = endpointContext.Endpoint.Binding.CreateBindingElements();
188 if (wsdlTransportUri != null)
190 WsdlNS.SoapBinding soapBinding = SoapHelper.GetOrCreateSoapBinding(endpointContext, exporter);
192 if (soapBinding != null)
194 soapBinding.Transport = wsdlTransportUri;
198 if (endpointContext.WsdlPort != null)
200 WsdlExporter.WSAddressingHelper.AddAddressToWsdlPort(endpointContext.WsdlPort, address, addressingVersion);
203 internal override bool IsMatch(BindingElement b)
209 TransportBindingElement transport = b as TransportBindingElement;
210 if (transport == null)
214 if (this.maxBufferPoolSize != transport.MaxBufferPoolSize)
218 if (this.maxReceivedMessageSize != transport.MaxReceivedMessageSize)