| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- //------------------------------------------------------------
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //------------------------------------------------------------
- namespace System.ServiceModel
- {
- using System;
- using System.Text;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Configuration;
- using System.Globalization;
- using System.Net;
- using System.Net.Security;
- using System.Runtime.Serialization;
- using System.Security.Principal;
- using System.ServiceModel.Channels;
- using System.ServiceModel.Configuration;
- using System.ServiceModel.Security;
- using System.Xml;
- using System.Net.Sockets;
- using System.ComponentModel;
- public class NetNamedPipeBinding : Binding, IBindingRuntimePreferences
- {
- // private BindingElements
- TransactionFlowBindingElement context;
- BinaryMessageEncodingBindingElement encoding;
- NamedPipeTransportBindingElement namedPipe;
- NetNamedPipeSecurity security = new NetNamedPipeSecurity();
- public NetNamedPipeBinding()
- : base()
- {
- Initialize();
- }
- public NetNamedPipeBinding(NetNamedPipeSecurityMode securityMode)
- : this()
- {
- this.security.Mode = securityMode;
- }
- public NetNamedPipeBinding(string configurationName)
- : this()
- {
- ApplyConfiguration(configurationName);
- }
- NetNamedPipeBinding(NetNamedPipeSecurity security)
- : this()
- {
- this.security = security;
- }
- [DefaultValue(TransactionFlowDefaults.Transactions)]
- public bool TransactionFlow
- {
- get { return context.Transactions; }
- set { context.Transactions = value; }
- }
- public TransactionProtocol TransactionProtocol
- {
- get { return this.context.TransactionProtocol; }
- set { this.context.TransactionProtocol = value; }
- }
- [DefaultValue(ConnectionOrientedTransportDefaults.TransferMode)]
- public TransferMode TransferMode
- {
- get { return namedPipe.TransferMode; }
- set { namedPipe.TransferMode = value; }
- }
- [DefaultValue(ConnectionOrientedTransportDefaults.HostNameComparisonMode)]
- public HostNameComparisonMode HostNameComparisonMode
- {
- get { return namedPipe.HostNameComparisonMode; }
- set { namedPipe.HostNameComparisonMode = value; }
- }
- [DefaultValue(TransportDefaults.MaxBufferPoolSize)]
- public long MaxBufferPoolSize
- {
- get { return namedPipe.MaxBufferPoolSize; }
- set
- {
- namedPipe.MaxBufferPoolSize = value;
- }
- }
- [DefaultValue(TransportDefaults.MaxBufferSize)]
- public int MaxBufferSize
- {
- get { return namedPipe.MaxBufferSize; }
- set { namedPipe.MaxBufferSize = value; }
- }
- public int MaxConnections
- {
- get { return namedPipe.MaxPendingConnections; }
- set
- {
- namedPipe.MaxPendingConnections = value;
- namedPipe.ConnectionPoolSettings.MaxOutboundConnectionsPerEndpoint = value;
- }
- }
- internal bool IsMaxConnectionsSet
- {
- get { return namedPipe.IsMaxPendingConnectionsSet; }
- }
- [DefaultValue(TransportDefaults.MaxReceivedMessageSize)]
- public long MaxReceivedMessageSize
- {
- get { return namedPipe.MaxReceivedMessageSize; }
- set { namedPipe.MaxReceivedMessageSize = value; }
- }
- public XmlDictionaryReaderQuotas ReaderQuotas
- {
- get { return encoding.ReaderQuotas; }
- set
- {
- if (value == null)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
- value.CopyTo(encoding.ReaderQuotas);
- }
- }
- bool IBindingRuntimePreferences.ReceiveSynchronously
- {
- get { return false; }
- }
- public override string Scheme { get { return namedPipe.Scheme; } }
- public EnvelopeVersion EnvelopeVersion
- {
- get { return EnvelopeVersion.Soap12; }
- }
- public NetNamedPipeSecurity Security
- {
- get { return this.security; }
- set
- {
- if (value == null)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
- this.security = value;
- }
- }
- static TransactionFlowBindingElement GetDefaultTransactionFlowBindingElement()
- {
- return new TransactionFlowBindingElement(false);
- }
- void Initialize()
- {
- namedPipe = new NamedPipeTransportBindingElement();
- encoding = new BinaryMessageEncodingBindingElement();
- context = GetDefaultTransactionFlowBindingElement();
- }
- void InitializeFrom(NamedPipeTransportBindingElement namedPipe, BinaryMessageEncodingBindingElement encoding, TransactionFlowBindingElement context)
- {
- Initialize();
- this.HostNameComparisonMode = namedPipe.HostNameComparisonMode;
- this.MaxBufferPoolSize = namedPipe.MaxBufferPoolSize;
- this.MaxBufferSize = namedPipe.MaxBufferSize;
- if (namedPipe.IsMaxPendingConnectionsSet)
- {
- this.MaxConnections = namedPipe.MaxPendingConnections;
- }
- this.MaxReceivedMessageSize = namedPipe.MaxReceivedMessageSize;
- this.TransferMode = namedPipe.TransferMode;
- this.ReaderQuotas = encoding.ReaderQuotas;
- this.TransactionFlow = context.Transactions;
- this.TransactionProtocol = context.TransactionProtocol;
- }
- // check that properties of the HttpTransportBindingElement and
- // MessageEncodingBindingElement not exposed as properties on BasicHttpBinding
- // match default values of the binding elements
- bool IsBindingElementsMatch(NamedPipeTransportBindingElement namedPipe, BinaryMessageEncodingBindingElement encoding, TransactionFlowBindingElement context)
- {
- if (!this.namedPipe.IsMatch(namedPipe))
- return false;
- if (!this.encoding.IsMatch(encoding))
- return false;
- if (!this.context.IsMatch(context))
- return false;
- return true;
- }
- void ApplyConfiguration(string configurationName)
- {
- NetNamedPipeBindingCollectionElement section = NetNamedPipeBindingCollectionElement.GetBindingCollectionElement();
- NetNamedPipeBindingElement element = section.Bindings[configurationName];
- if (element == null)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
- SR.GetString(SR.ConfigInvalidBindingConfigurationName,
- configurationName,
- ConfigurationStrings.NetNamedPipeBindingCollectionElementName)));
- }
- else
- {
- element.ApplyConfiguration(this);
- }
- }
- public override BindingElementCollection CreateBindingElements()
- { // return collection of BindingElements
- BindingElementCollection bindingElements = new BindingElementCollection();
- // order of BindingElements is important
- // add context
- bindingElements.Add(context);
- // add encoding
- bindingElements.Add(encoding);
- // add transport security
- WindowsStreamSecurityBindingElement transportSecurity = CreateTransportSecurity();
- if (transportSecurity != null)
- {
- bindingElements.Add(transportSecurity);
- }
- // add transport (named pipes)
- bindingElements.Add(this.namedPipe);
- return bindingElements.Clone();
- }
- internal static bool TryCreate(BindingElementCollection elements, out Binding binding)
- {
- binding = null;
- if (elements.Count > 4)
- return false;
- TransactionFlowBindingElement context = null;
- BinaryMessageEncodingBindingElement encoding = null;
- WindowsStreamSecurityBindingElement security = null;
- NamedPipeTransportBindingElement namedPipe = null;
- foreach (BindingElement element in elements)
- {
- if (element is TransactionFlowBindingElement)
- context = element as TransactionFlowBindingElement;
- else if (element is BinaryMessageEncodingBindingElement)
- encoding = element as BinaryMessageEncodingBindingElement;
- else if (element is WindowsStreamSecurityBindingElement)
- security = element as WindowsStreamSecurityBindingElement;
- else if (element is NamedPipeTransportBindingElement)
- namedPipe = element as NamedPipeTransportBindingElement;
- else
- return false;
- }
- if (namedPipe == null)
- return false;
- if (encoding == null)
- return false;
- if (context == null)
- context = GetDefaultTransactionFlowBindingElement();
- NetNamedPipeSecurity pipeSecurity;
- if (!TryCreateSecurity(security, out pipeSecurity))
- return false;
- NetNamedPipeBinding netNamedPipeBinding = new NetNamedPipeBinding(pipeSecurity);
- netNamedPipeBinding.InitializeFrom(namedPipe, encoding, context);
- if (!netNamedPipeBinding.IsBindingElementsMatch(namedPipe, encoding, context))
- return false;
- binding = netNamedPipeBinding;
- return true;
- }
- WindowsStreamSecurityBindingElement CreateTransportSecurity()
- {
- if (this.security.Mode == NetNamedPipeSecurityMode.Transport)
- {
- return this.security.CreateTransportSecurity();
- }
- else
- {
- return null;
- }
- }
- static bool TryCreateSecurity(WindowsStreamSecurityBindingElement wssbe, out NetNamedPipeSecurity security)
- {
- NetNamedPipeSecurityMode mode = wssbe == null ? NetNamedPipeSecurityMode.None : NetNamedPipeSecurityMode.Transport;
- return NetNamedPipeSecurity.TryCreate(wssbe, mode, out security);
- }
- [EditorBrowsable(EditorBrowsableState.Never)]
- public bool ShouldSerializeReaderQuotas()
- {
- return (!EncoderDefaults.IsDefaultReaderQuotas(this.ReaderQuotas));
- }
- [EditorBrowsable(EditorBrowsableState.Never)]
- public bool ShouldSerializeSecurity()
- {
- if (this.security.Mode != NetNamedPipeSecurity.DefaultMode)
- {
- return true;
- }
- if (this.security.Transport.ProtectionLevel != NamedPipeTransportSecurity.DefaultProtectionLevel)
- {
- return true;
- }
- return false;
- }
- [EditorBrowsable(EditorBrowsableState.Never)]
- public bool ShouldSerializeTransactionProtocol()
- {
- return (TransactionProtocol != NetTcpDefaults.TransactionProtocol);
- }
- [EditorBrowsable(EditorBrowsableState.Never)]
- public bool ShouldSerializeMaxConnections()
- {
- return namedPipe.ShouldSerializeMaxPendingConnections();
- }
- }
- }
|