NetNamedPipeBinding.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System;
  7. using System.Text;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Configuration;
  11. using System.Globalization;
  12. using System.Net;
  13. using System.Net.Security;
  14. using System.Runtime.Serialization;
  15. using System.Security.Principal;
  16. using System.ServiceModel.Channels;
  17. using System.ServiceModel.Configuration;
  18. using System.ServiceModel.Security;
  19. using System.Xml;
  20. using System.Net.Sockets;
  21. using System.ComponentModel;
  22. public class NetNamedPipeBinding : Binding, IBindingRuntimePreferences
  23. {
  24. // private BindingElements
  25. TransactionFlowBindingElement context;
  26. BinaryMessageEncodingBindingElement encoding;
  27. NamedPipeTransportBindingElement namedPipe;
  28. NetNamedPipeSecurity security = new NetNamedPipeSecurity();
  29. public NetNamedPipeBinding()
  30. : base()
  31. {
  32. Initialize();
  33. }
  34. public NetNamedPipeBinding(NetNamedPipeSecurityMode securityMode)
  35. : this()
  36. {
  37. this.security.Mode = securityMode;
  38. }
  39. public NetNamedPipeBinding(string configurationName)
  40. : this()
  41. {
  42. ApplyConfiguration(configurationName);
  43. }
  44. NetNamedPipeBinding(NetNamedPipeSecurity security)
  45. : this()
  46. {
  47. this.security = security;
  48. }
  49. [DefaultValue(TransactionFlowDefaults.Transactions)]
  50. public bool TransactionFlow
  51. {
  52. get { return context.Transactions; }
  53. set { context.Transactions = value; }
  54. }
  55. public TransactionProtocol TransactionProtocol
  56. {
  57. get { return this.context.TransactionProtocol; }
  58. set { this.context.TransactionProtocol = value; }
  59. }
  60. [DefaultValue(ConnectionOrientedTransportDefaults.TransferMode)]
  61. public TransferMode TransferMode
  62. {
  63. get { return namedPipe.TransferMode; }
  64. set { namedPipe.TransferMode = value; }
  65. }
  66. [DefaultValue(ConnectionOrientedTransportDefaults.HostNameComparisonMode)]
  67. public HostNameComparisonMode HostNameComparisonMode
  68. {
  69. get { return namedPipe.HostNameComparisonMode; }
  70. set { namedPipe.HostNameComparisonMode = value; }
  71. }
  72. [DefaultValue(TransportDefaults.MaxBufferPoolSize)]
  73. public long MaxBufferPoolSize
  74. {
  75. get { return namedPipe.MaxBufferPoolSize; }
  76. set
  77. {
  78. namedPipe.MaxBufferPoolSize = value;
  79. }
  80. }
  81. [DefaultValue(TransportDefaults.MaxBufferSize)]
  82. public int MaxBufferSize
  83. {
  84. get { return namedPipe.MaxBufferSize; }
  85. set { namedPipe.MaxBufferSize = value; }
  86. }
  87. public int MaxConnections
  88. {
  89. get { return namedPipe.MaxPendingConnections; }
  90. set
  91. {
  92. namedPipe.MaxPendingConnections = value;
  93. namedPipe.ConnectionPoolSettings.MaxOutboundConnectionsPerEndpoint = value;
  94. }
  95. }
  96. internal bool IsMaxConnectionsSet
  97. {
  98. get { return namedPipe.IsMaxPendingConnectionsSet; }
  99. }
  100. [DefaultValue(TransportDefaults.MaxReceivedMessageSize)]
  101. public long MaxReceivedMessageSize
  102. {
  103. get { return namedPipe.MaxReceivedMessageSize; }
  104. set { namedPipe.MaxReceivedMessageSize = value; }
  105. }
  106. public XmlDictionaryReaderQuotas ReaderQuotas
  107. {
  108. get { return encoding.ReaderQuotas; }
  109. set
  110. {
  111. if (value == null)
  112. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  113. value.CopyTo(encoding.ReaderQuotas);
  114. }
  115. }
  116. bool IBindingRuntimePreferences.ReceiveSynchronously
  117. {
  118. get { return false; }
  119. }
  120. public override string Scheme { get { return namedPipe.Scheme; } }
  121. public EnvelopeVersion EnvelopeVersion
  122. {
  123. get { return EnvelopeVersion.Soap12; }
  124. }
  125. public NetNamedPipeSecurity Security
  126. {
  127. get { return this.security; }
  128. set
  129. {
  130. if (value == null)
  131. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  132. this.security = value;
  133. }
  134. }
  135. static TransactionFlowBindingElement GetDefaultTransactionFlowBindingElement()
  136. {
  137. return new TransactionFlowBindingElement(false);
  138. }
  139. void Initialize()
  140. {
  141. namedPipe = new NamedPipeTransportBindingElement();
  142. encoding = new BinaryMessageEncodingBindingElement();
  143. context = GetDefaultTransactionFlowBindingElement();
  144. }
  145. void InitializeFrom(NamedPipeTransportBindingElement namedPipe, BinaryMessageEncodingBindingElement encoding, TransactionFlowBindingElement context)
  146. {
  147. Initialize();
  148. this.HostNameComparisonMode = namedPipe.HostNameComparisonMode;
  149. this.MaxBufferPoolSize = namedPipe.MaxBufferPoolSize;
  150. this.MaxBufferSize = namedPipe.MaxBufferSize;
  151. if (namedPipe.IsMaxPendingConnectionsSet)
  152. {
  153. this.MaxConnections = namedPipe.MaxPendingConnections;
  154. }
  155. this.MaxReceivedMessageSize = namedPipe.MaxReceivedMessageSize;
  156. this.TransferMode = namedPipe.TransferMode;
  157. this.ReaderQuotas = encoding.ReaderQuotas;
  158. this.TransactionFlow = context.Transactions;
  159. this.TransactionProtocol = context.TransactionProtocol;
  160. }
  161. // check that properties of the HttpTransportBindingElement and
  162. // MessageEncodingBindingElement not exposed as properties on BasicHttpBinding
  163. // match default values of the binding elements
  164. bool IsBindingElementsMatch(NamedPipeTransportBindingElement namedPipe, BinaryMessageEncodingBindingElement encoding, TransactionFlowBindingElement context)
  165. {
  166. if (!this.namedPipe.IsMatch(namedPipe))
  167. return false;
  168. if (!this.encoding.IsMatch(encoding))
  169. return false;
  170. if (!this.context.IsMatch(context))
  171. return false;
  172. return true;
  173. }
  174. void ApplyConfiguration(string configurationName)
  175. {
  176. NetNamedPipeBindingCollectionElement section = NetNamedPipeBindingCollectionElement.GetBindingCollectionElement();
  177. NetNamedPipeBindingElement element = section.Bindings[configurationName];
  178. if (element == null)
  179. {
  180. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
  181. SR.GetString(SR.ConfigInvalidBindingConfigurationName,
  182. configurationName,
  183. ConfigurationStrings.NetNamedPipeBindingCollectionElementName)));
  184. }
  185. else
  186. {
  187. element.ApplyConfiguration(this);
  188. }
  189. }
  190. public override BindingElementCollection CreateBindingElements()
  191. { // return collection of BindingElements
  192. BindingElementCollection bindingElements = new BindingElementCollection();
  193. // order of BindingElements is important
  194. // add context
  195. bindingElements.Add(context);
  196. // add encoding
  197. bindingElements.Add(encoding);
  198. // add transport security
  199. WindowsStreamSecurityBindingElement transportSecurity = CreateTransportSecurity();
  200. if (transportSecurity != null)
  201. {
  202. bindingElements.Add(transportSecurity);
  203. }
  204. // add transport (named pipes)
  205. bindingElements.Add(this.namedPipe);
  206. return bindingElements.Clone();
  207. }
  208. internal static bool TryCreate(BindingElementCollection elements, out Binding binding)
  209. {
  210. binding = null;
  211. if (elements.Count > 4)
  212. return false;
  213. TransactionFlowBindingElement context = null;
  214. BinaryMessageEncodingBindingElement encoding = null;
  215. WindowsStreamSecurityBindingElement security = null;
  216. NamedPipeTransportBindingElement namedPipe = null;
  217. foreach (BindingElement element in elements)
  218. {
  219. if (element is TransactionFlowBindingElement)
  220. context = element as TransactionFlowBindingElement;
  221. else if (element is BinaryMessageEncodingBindingElement)
  222. encoding = element as BinaryMessageEncodingBindingElement;
  223. else if (element is WindowsStreamSecurityBindingElement)
  224. security = element as WindowsStreamSecurityBindingElement;
  225. else if (element is NamedPipeTransportBindingElement)
  226. namedPipe = element as NamedPipeTransportBindingElement;
  227. else
  228. return false;
  229. }
  230. if (namedPipe == null)
  231. return false;
  232. if (encoding == null)
  233. return false;
  234. if (context == null)
  235. context = GetDefaultTransactionFlowBindingElement();
  236. NetNamedPipeSecurity pipeSecurity;
  237. if (!TryCreateSecurity(security, out pipeSecurity))
  238. return false;
  239. NetNamedPipeBinding netNamedPipeBinding = new NetNamedPipeBinding(pipeSecurity);
  240. netNamedPipeBinding.InitializeFrom(namedPipe, encoding, context);
  241. if (!netNamedPipeBinding.IsBindingElementsMatch(namedPipe, encoding, context))
  242. return false;
  243. binding = netNamedPipeBinding;
  244. return true;
  245. }
  246. WindowsStreamSecurityBindingElement CreateTransportSecurity()
  247. {
  248. if (this.security.Mode == NetNamedPipeSecurityMode.Transport)
  249. {
  250. return this.security.CreateTransportSecurity();
  251. }
  252. else
  253. {
  254. return null;
  255. }
  256. }
  257. static bool TryCreateSecurity(WindowsStreamSecurityBindingElement wssbe, out NetNamedPipeSecurity security)
  258. {
  259. NetNamedPipeSecurityMode mode = wssbe == null ? NetNamedPipeSecurityMode.None : NetNamedPipeSecurityMode.Transport;
  260. return NetNamedPipeSecurity.TryCreate(wssbe, mode, out security);
  261. }
  262. [EditorBrowsable(EditorBrowsableState.Never)]
  263. public bool ShouldSerializeReaderQuotas()
  264. {
  265. return (!EncoderDefaults.IsDefaultReaderQuotas(this.ReaderQuotas));
  266. }
  267. [EditorBrowsable(EditorBrowsableState.Never)]
  268. public bool ShouldSerializeSecurity()
  269. {
  270. if (this.security.Mode != NetNamedPipeSecurity.DefaultMode)
  271. {
  272. return true;
  273. }
  274. if (this.security.Transport.ProtectionLevel != NamedPipeTransportSecurity.DefaultProtectionLevel)
  275. {
  276. return true;
  277. }
  278. return false;
  279. }
  280. [EditorBrowsable(EditorBrowsableState.Never)]
  281. public bool ShouldSerializeTransactionProtocol()
  282. {
  283. return (TransactionProtocol != NetTcpDefaults.TransactionProtocol);
  284. }
  285. [EditorBrowsable(EditorBrowsableState.Never)]
  286. public bool ShouldSerializeMaxConnections()
  287. {
  288. return namedPipe.ShouldSerializeMaxPendingConnections();
  289. }
  290. }
  291. }