NetTcpBinding.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.ComponentModel;
  7. using System.Configuration;
  8. using System.Runtime;
  9. using System.ServiceModel.Channels;
  10. using System.ServiceModel.Configuration;
  11. using System.Xml;
  12. public class NetTcpBinding : Binding, IBindingRuntimePreferences
  13. {
  14. OptionalReliableSession reliableSession;
  15. // private BindingElements
  16. TcpTransportBindingElement transport;
  17. BinaryMessageEncodingBindingElement encoding;
  18. TransactionFlowBindingElement context;
  19. ReliableSessionBindingElement session;
  20. NetTcpSecurity security = new NetTcpSecurity();
  21. public NetTcpBinding() { Initialize(); }
  22. public NetTcpBinding(SecurityMode securityMode)
  23. : this()
  24. {
  25. this.security.Mode = securityMode;
  26. }
  27. public NetTcpBinding(SecurityMode securityMode, bool reliableSessionEnabled)
  28. : this(securityMode)
  29. {
  30. this.ReliableSession.Enabled = reliableSessionEnabled;
  31. }
  32. public NetTcpBinding(string configurationName)
  33. : this()
  34. {
  35. ApplyConfiguration(configurationName);
  36. }
  37. NetTcpBinding(TcpTransportBindingElement transport, BinaryMessageEncodingBindingElement encoding, TransactionFlowBindingElement context, ReliableSessionBindingElement session, NetTcpSecurity security)
  38. : this()
  39. {
  40. this.security = security;
  41. this.ReliableSession.Enabled = session != null;
  42. InitializeFrom(transport, encoding, context, session);
  43. }
  44. [DefaultValue(NetTcpDefaults.TransactionsEnabled)]
  45. public bool TransactionFlow
  46. {
  47. get { return context.Transactions; }
  48. set { context.Transactions = value; }
  49. }
  50. public TransactionProtocol TransactionProtocol
  51. {
  52. get { return this.context.TransactionProtocol; }
  53. set { this.context.TransactionProtocol = value; }
  54. }
  55. [DefaultValue(ConnectionOrientedTransportDefaults.TransferMode)]
  56. public TransferMode TransferMode
  57. {
  58. get { return this.transport.TransferMode; }
  59. set { this.transport.TransferMode = value; }
  60. }
  61. [DefaultValue(ConnectionOrientedTransportDefaults.HostNameComparisonMode)]
  62. public HostNameComparisonMode HostNameComparisonMode
  63. {
  64. get { return transport.HostNameComparisonMode; }
  65. set { transport.HostNameComparisonMode = value; }
  66. }
  67. [DefaultValue(TransportDefaults.MaxBufferPoolSize)]
  68. public long MaxBufferPoolSize
  69. {
  70. get { return transport.MaxBufferPoolSize; }
  71. set
  72. {
  73. transport.MaxBufferPoolSize = value;
  74. }
  75. }
  76. [DefaultValue(TransportDefaults.MaxBufferSize)]
  77. public int MaxBufferSize
  78. {
  79. get { return transport.MaxBufferSize; }
  80. set { transport.MaxBufferSize = value; }
  81. }
  82. public int MaxConnections
  83. {
  84. get { return transport.MaxPendingConnections; }
  85. set
  86. {
  87. transport.MaxPendingConnections = value;
  88. transport.ConnectionPoolSettings.MaxOutboundConnectionsPerEndpoint = value;
  89. }
  90. }
  91. internal bool IsMaxConnectionsSet
  92. {
  93. get { return transport.IsMaxPendingConnectionsSet; }
  94. }
  95. public int ListenBacklog
  96. {
  97. get { return transport.ListenBacklog; }
  98. set { transport.ListenBacklog = value; }
  99. }
  100. internal bool IsListenBacklogSet
  101. {
  102. get { return transport.IsListenBacklogSet; }
  103. }
  104. [DefaultValue(TransportDefaults.MaxReceivedMessageSize)]
  105. public long MaxReceivedMessageSize
  106. {
  107. get { return transport.MaxReceivedMessageSize; }
  108. set { transport.MaxReceivedMessageSize = value; }
  109. }
  110. [DefaultValue(TcpTransportDefaults.PortSharingEnabled)]
  111. public bool PortSharingEnabled
  112. {
  113. get { return transport.PortSharingEnabled; }
  114. set { transport.PortSharingEnabled = value; }
  115. }
  116. public XmlDictionaryReaderQuotas ReaderQuotas
  117. {
  118. get { return encoding.ReaderQuotas; }
  119. set
  120. {
  121. if (value == null)
  122. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  123. value.CopyTo(encoding.ReaderQuotas);
  124. }
  125. }
  126. bool IBindingRuntimePreferences.ReceiveSynchronously
  127. {
  128. get { return false; }
  129. }
  130. public OptionalReliableSession ReliableSession
  131. {
  132. get
  133. {
  134. return reliableSession;
  135. }
  136. set
  137. {
  138. if (value == null)
  139. {
  140. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("value"));
  141. }
  142. this.reliableSession.CopySettings(value);
  143. }
  144. }
  145. public override string Scheme { get { return transport.Scheme; } }
  146. public EnvelopeVersion EnvelopeVersion
  147. {
  148. get { return EnvelopeVersion.Soap12; }
  149. }
  150. public NetTcpSecurity Security
  151. {
  152. get { return security; }
  153. set
  154. {
  155. if (value == null)
  156. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  157. security = value;
  158. }
  159. }
  160. static TransactionFlowBindingElement GetDefaultTransactionFlowBindingElement()
  161. {
  162. return new TransactionFlowBindingElement(NetTcpDefaults.TransactionsEnabled);
  163. }
  164. void Initialize()
  165. {
  166. transport = new TcpTransportBindingElement();
  167. encoding = new BinaryMessageEncodingBindingElement();
  168. context = GetDefaultTransactionFlowBindingElement();
  169. session = new ReliableSessionBindingElement();
  170. this.reliableSession = new OptionalReliableSession(session);
  171. }
  172. void InitializeFrom(TcpTransportBindingElement transport, BinaryMessageEncodingBindingElement encoding, TransactionFlowBindingElement context, ReliableSessionBindingElement session)
  173. {
  174. Fx.Assert(transport != null, "Invalid (null) transport value.");
  175. Fx.Assert(encoding != null, "Invalid (null) encoding value.");
  176. Fx.Assert(context != null, "Invalid (null) context value.");
  177. Fx.Assert(security != null, "Invalid (null) security value.");
  178. // transport
  179. this.HostNameComparisonMode = transport.HostNameComparisonMode;
  180. this.MaxBufferPoolSize = transport.MaxBufferPoolSize;
  181. this.MaxBufferSize = transport.MaxBufferSize;
  182. if (transport.IsMaxPendingConnectionsSet)
  183. {
  184. this.MaxConnections = transport.MaxPendingConnections;
  185. }
  186. if (transport.IsListenBacklogSet)
  187. {
  188. this.ListenBacklog = transport.ListenBacklog;
  189. }
  190. this.MaxReceivedMessageSize = transport.MaxReceivedMessageSize;
  191. this.PortSharingEnabled = transport.PortSharingEnabled;
  192. this.TransferMode = transport.TransferMode;
  193. // encoding
  194. this.ReaderQuotas = encoding.ReaderQuotas;
  195. // context
  196. this.TransactionFlow = context.Transactions;
  197. this.TransactionProtocol = context.TransactionProtocol;
  198. //session
  199. if (session != null)
  200. {
  201. // only set properties that have standard binding manifestations
  202. this.session.InactivityTimeout = session.InactivityTimeout;
  203. this.session.Ordered = session.Ordered;
  204. }
  205. }
  206. // check that properties of the HttpTransportBindingElement and
  207. // MessageEncodingBindingElement not exposed as properties on BasicHttpBinding
  208. // match default values of the binding elements
  209. bool IsBindingElementsMatch(TcpTransportBindingElement transport, BinaryMessageEncodingBindingElement encoding, TransactionFlowBindingElement context, ReliableSessionBindingElement session)
  210. {
  211. if (!this.transport.IsMatch(transport))
  212. return false;
  213. if (!this.encoding.IsMatch(encoding))
  214. return false;
  215. if (!this.context.IsMatch(context))
  216. return false;
  217. if (reliableSession.Enabled)
  218. {
  219. if (!this.session.IsMatch(session))
  220. return false;
  221. }
  222. else if (session != null)
  223. return false;
  224. return true;
  225. }
  226. void ApplyConfiguration(string configurationName)
  227. {
  228. NetTcpBindingCollectionElement section = NetTcpBindingCollectionElement.GetBindingCollectionElement();
  229. NetTcpBindingElement element = section.Bindings[configurationName];
  230. if (element == null)
  231. {
  232. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
  233. SR.GetString(SR.ConfigInvalidBindingConfigurationName,
  234. configurationName,
  235. ConfigurationStrings.NetTcpBindingCollectionElementName)));
  236. }
  237. else
  238. {
  239. element.ApplyConfiguration(this);
  240. }
  241. }
  242. // In the Win8 profile, some settings for the binding security are not supported.
  243. void CheckSettings()
  244. {
  245. if (!UnsafeNativeMethods.IsTailoredApplication.Value)
  246. {
  247. return;
  248. }
  249. NetTcpSecurity security = this.Security;
  250. if (security == null)
  251. {
  252. return;
  253. }
  254. SecurityMode mode = security.Mode;
  255. if (mode == SecurityMode.None)
  256. {
  257. return;
  258. }
  259. else if (mode == SecurityMode.Message)
  260. {
  261. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.UnsupportedSecuritySetting, "Mode", mode)));
  262. }
  263. // Message.ClientCredentialType = Certificate, IssuedToken or Windows are not supported.
  264. if (mode == SecurityMode.TransportWithMessageCredential)
  265. {
  266. MessageSecurityOverTcp message = security.Message;
  267. if (message != null)
  268. {
  269. MessageCredentialType mct = message.ClientCredentialType;
  270. if ((mct == MessageCredentialType.Certificate) || (mct == MessageCredentialType.IssuedToken) || (mct == MessageCredentialType.Windows))
  271. {
  272. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.UnsupportedSecuritySetting, "Message.ClientCredentialType", mct)));
  273. }
  274. }
  275. }
  276. // Transport.ClientCredentialType = Certificate is not supported.
  277. Fx.Assert((mode == SecurityMode.Transport) || (mode == SecurityMode.TransportWithMessageCredential), "Unexpected SecurityMode value: " + mode);
  278. TcpTransportSecurity transport = security.Transport;
  279. if ((transport != null) && (transport.ClientCredentialType == TcpClientCredentialType.Certificate))
  280. {
  281. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.UnsupportedSecuritySetting, "Transport.ClientCredentialType", transport.ClientCredentialType)));
  282. }
  283. }
  284. public override BindingElementCollection CreateBindingElements()
  285. {
  286. this.CheckSettings();
  287. // return collection of BindingElements
  288. BindingElementCollection bindingElements = new BindingElementCollection();
  289. // order of BindingElements is important
  290. // add context
  291. bindingElements.Add(context);
  292. // add session
  293. if (reliableSession.Enabled)
  294. bindingElements.Add(session);
  295. // add security (*optional)
  296. SecurityBindingElement wsSecurity = CreateMessageSecurity();
  297. if (wsSecurity != null)
  298. bindingElements.Add(wsSecurity);
  299. // add encoding
  300. bindingElements.Add(encoding);
  301. // add transport security
  302. BindingElement transportSecurity = CreateTransportSecurity();
  303. if (transportSecurity != null)
  304. {
  305. bindingElements.Add(transportSecurity);
  306. }
  307. transport.ExtendedProtectionPolicy = security.Transport.ExtendedProtectionPolicy;
  308. // add transport (tcp)
  309. bindingElements.Add(transport);
  310. return bindingElements.Clone();
  311. }
  312. internal static bool TryCreate(BindingElementCollection elements, out Binding binding)
  313. {
  314. binding = null;
  315. if (elements.Count > 6)
  316. return false;
  317. // collect all binding elements
  318. TcpTransportBindingElement transport = null;
  319. BinaryMessageEncodingBindingElement encoding = null;
  320. TransactionFlowBindingElement context = null;
  321. ReliableSessionBindingElement session = null;
  322. SecurityBindingElement wsSecurity = null;
  323. BindingElement transportSecurity = null;
  324. foreach (BindingElement element in elements)
  325. {
  326. if (element is SecurityBindingElement)
  327. wsSecurity = element as SecurityBindingElement;
  328. else if (element is TransportBindingElement)
  329. transport = element as TcpTransportBindingElement;
  330. else if (element is MessageEncodingBindingElement)
  331. encoding = element as BinaryMessageEncodingBindingElement;
  332. else if (element is TransactionFlowBindingElement)
  333. context = element as TransactionFlowBindingElement;
  334. else if (element is ReliableSessionBindingElement)
  335. session = element as ReliableSessionBindingElement;
  336. else
  337. {
  338. if (transportSecurity != null)
  339. return false;
  340. transportSecurity = element;
  341. }
  342. }
  343. if (transport == null)
  344. return false;
  345. if (encoding == null)
  346. return false;
  347. if (context == null)
  348. context = GetDefaultTransactionFlowBindingElement();
  349. TcpTransportSecurity tcpTransportSecurity = new TcpTransportSecurity();
  350. UnifiedSecurityMode mode = GetModeFromTransportSecurity(transportSecurity);
  351. NetTcpSecurity security;
  352. if (!TryCreateSecurity(wsSecurity, mode, session != null, transportSecurity, tcpTransportSecurity, out security))
  353. return false;
  354. if (!SetTransportSecurity(transportSecurity, security.Mode, tcpTransportSecurity))
  355. return false;
  356. NetTcpBinding netTcpBinding = new NetTcpBinding(transport, encoding, context, session, security);
  357. if (!netTcpBinding.IsBindingElementsMatch(transport, encoding, context, session))
  358. return false;
  359. binding = netTcpBinding;
  360. return true;
  361. }
  362. BindingElement CreateTransportSecurity()
  363. {
  364. return this.security.CreateTransportSecurity();
  365. }
  366. static UnifiedSecurityMode GetModeFromTransportSecurity(BindingElement transport)
  367. {
  368. return NetTcpSecurity.GetModeFromTransportSecurity(transport);
  369. }
  370. static bool SetTransportSecurity(BindingElement transport, SecurityMode mode, TcpTransportSecurity transportSecurity)
  371. {
  372. return NetTcpSecurity.SetTransportSecurity(transport, mode, transportSecurity);
  373. }
  374. SecurityBindingElement CreateMessageSecurity()
  375. {
  376. if (this.security.Mode == SecurityMode.Message || this.security.Mode == SecurityMode.TransportWithMessageCredential)
  377. {
  378. return this.security.CreateMessageSecurity(this.ReliableSession.Enabled);
  379. }
  380. else
  381. {
  382. return null;
  383. }
  384. }
  385. static bool TryCreateSecurity(SecurityBindingElement sbe, UnifiedSecurityMode mode, bool isReliableSession, BindingElement transportSecurity, TcpTransportSecurity tcpTransportSecurity, out NetTcpSecurity security)
  386. {
  387. if (sbe != null)
  388. mode &= UnifiedSecurityMode.Message | UnifiedSecurityMode.TransportWithMessageCredential;
  389. else
  390. mode &= ~(UnifiedSecurityMode.Message | UnifiedSecurityMode.TransportWithMessageCredential);
  391. SecurityMode securityMode = SecurityModeHelper.ToSecurityMode(mode);
  392. Fx.Assert(SecurityModeHelper.IsDefined(securityMode), string.Format("Invalid SecurityMode value: {0}.", securityMode.ToString()));
  393. if (NetTcpSecurity.TryCreate(sbe, securityMode, isReliableSession, transportSecurity, tcpTransportSecurity, out security))
  394. return true;
  395. return false;
  396. }
  397. [EditorBrowsable(EditorBrowsableState.Never)]
  398. public bool ShouldSerializeReaderQuotas()
  399. {
  400. return (!EncoderDefaults.IsDefaultReaderQuotas(this.ReaderQuotas));
  401. }
  402. [EditorBrowsable(EditorBrowsableState.Never)]
  403. public bool ShouldSerializeSecurity()
  404. {
  405. return this.security.InternalShouldSerialize();
  406. }
  407. [EditorBrowsable(EditorBrowsableState.Never)]
  408. public bool ShouldSerializeTransactionProtocol()
  409. {
  410. return (TransactionProtocol != NetTcpDefaults.TransactionProtocol);
  411. }
  412. [EditorBrowsable(EditorBrowsableState.Never)]
  413. public bool ShouldSerializeReliableSession()
  414. {
  415. return (this.ReliableSession.Ordered != ReliableSessionDefaults.Ordered
  416. || this.ReliableSession.InactivityTimeout != ReliableSessionDefaults.InactivityTimeout
  417. || this.ReliableSession.Enabled != ReliableSessionDefaults.Enabled);
  418. }
  419. [EditorBrowsable(EditorBrowsableState.Never)]
  420. public bool ShouldSerializeListenBacklog()
  421. {
  422. return transport.ShouldSerializeListenBacklog();
  423. }
  424. [EditorBrowsable(EditorBrowsableState.Never)]
  425. public bool ShouldSerializeMaxConnections()
  426. {
  427. return transport.ShouldSerializeListenBacklog();
  428. }
  429. }
  430. }