ReliableSessionBindingElement.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.ComponentModel;
  7. using System.Runtime;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Description;
  10. using System.ServiceModel.Dispatcher;
  11. using System.ServiceModel.Security;
  12. using System.Xml;
  13. public sealed class ReliableSessionBindingElement : BindingElement, IPolicyExportExtension
  14. {
  15. TimeSpan acknowledgementInterval = ReliableSessionDefaults.AcknowledgementInterval;
  16. bool flowControlEnabled = ReliableSessionDefaults.FlowControlEnabled;
  17. TimeSpan inactivityTimeout = ReliableSessionDefaults.InactivityTimeout;
  18. int maxPendingChannels = ReliableSessionDefaults.MaxPendingChannels;
  19. int maxRetryCount = ReliableSessionDefaults.MaxRetryCount;
  20. int maxTransferWindowSize = ReliableSessionDefaults.MaxTransferWindowSize;
  21. bool ordered = ReliableSessionDefaults.Ordered;
  22. ReliableMessagingVersion reliableMessagingVersion = ReliableMessagingVersion.Default;
  23. InternalDuplexBindingElement internalDuplexBindingElement;
  24. static MessagePartSpecification bodyOnly;
  25. public ReliableSessionBindingElement()
  26. {
  27. }
  28. internal ReliableSessionBindingElement(ReliableSessionBindingElement elementToBeCloned)
  29. : base(elementToBeCloned)
  30. {
  31. this.AcknowledgementInterval = elementToBeCloned.AcknowledgementInterval;
  32. this.FlowControlEnabled = elementToBeCloned.FlowControlEnabled;
  33. this.InactivityTimeout = elementToBeCloned.InactivityTimeout;
  34. this.MaxPendingChannels = elementToBeCloned.MaxPendingChannels;
  35. this.MaxRetryCount = elementToBeCloned.MaxRetryCount;
  36. this.MaxTransferWindowSize = elementToBeCloned.MaxTransferWindowSize;
  37. this.Ordered = elementToBeCloned.Ordered;
  38. this.ReliableMessagingVersion = elementToBeCloned.ReliableMessagingVersion;
  39. this.internalDuplexBindingElement = elementToBeCloned.internalDuplexBindingElement;
  40. }
  41. public ReliableSessionBindingElement(bool ordered)
  42. {
  43. this.ordered = ordered;
  44. }
  45. [DefaultValue(typeof(TimeSpan), ReliableSessionDefaults.AcknowledgementIntervalString)]
  46. public TimeSpan AcknowledgementInterval
  47. {
  48. get
  49. {
  50. return this.acknowledgementInterval;
  51. }
  52. set
  53. {
  54. if (value <= TimeSpan.Zero)
  55. {
  56. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  57. SR.GetString(SR.TimeSpanMustbeGreaterThanTimeSpanZero)));
  58. }
  59. if (TimeoutHelper.IsTooLarge(value))
  60. {
  61. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  62. SR.GetString(SR.SFxTimeoutOutOfRangeTooBig)));
  63. }
  64. this.acknowledgementInterval = value;
  65. }
  66. }
  67. [DefaultValue(ReliableSessionDefaults.FlowControlEnabled)]
  68. public bool FlowControlEnabled
  69. {
  70. get
  71. {
  72. return this.flowControlEnabled;
  73. }
  74. set
  75. {
  76. this.flowControlEnabled = value;
  77. }
  78. }
  79. [DefaultValue(typeof(TimeSpan), ReliableSessionDefaults.InactivityTimeoutString)]
  80. public TimeSpan InactivityTimeout
  81. {
  82. get
  83. {
  84. return this.inactivityTimeout;
  85. }
  86. set
  87. {
  88. if (value <= TimeSpan.Zero)
  89. {
  90. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  91. SR.GetString(SR.TimeSpanMustbeGreaterThanTimeSpanZero)));
  92. }
  93. if (TimeoutHelper.IsTooLarge(value))
  94. {
  95. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  96. SR.GetString(SR.SFxTimeoutOutOfRangeTooBig)));
  97. }
  98. this.inactivityTimeout = value;
  99. }
  100. }
  101. [DefaultValue(ReliableSessionDefaults.MaxPendingChannels)]
  102. public int MaxPendingChannels
  103. {
  104. get
  105. {
  106. return this.maxPendingChannels;
  107. }
  108. set
  109. {
  110. if (value <= 0 || value > 16384)
  111. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  112. SR.GetString(SR.ValueMustBeInRange, 0, 16384)));
  113. this.maxPendingChannels = value;
  114. }
  115. }
  116. [DefaultValue(ReliableSessionDefaults.MaxRetryCount)]
  117. public int MaxRetryCount
  118. {
  119. get
  120. {
  121. return this.maxRetryCount;
  122. }
  123. set
  124. {
  125. if (value <= 0)
  126. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  127. SR.GetString(SR.ValueMustBePositive)));
  128. this.maxRetryCount = value;
  129. }
  130. }
  131. [DefaultValue(ReliableSessionDefaults.MaxTransferWindowSize)]
  132. public int MaxTransferWindowSize
  133. {
  134. get
  135. {
  136. return this.maxTransferWindowSize;
  137. }
  138. set
  139. {
  140. if (value <= 0 || value > 4096)
  141. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  142. SR.GetString(SR.ValueMustBeInRange, 0, 4096)));
  143. this.maxTransferWindowSize = value;
  144. }
  145. }
  146. [DefaultValue(ReliableSessionDefaults.Ordered)]
  147. public bool Ordered
  148. {
  149. get
  150. {
  151. return this.ordered;
  152. }
  153. set
  154. {
  155. this.ordered = value;
  156. }
  157. }
  158. [DefaultValue(typeof(ReliableMessagingVersion), ReliableSessionDefaults.ReliableMessagingVersionString)]
  159. public ReliableMessagingVersion ReliableMessagingVersion
  160. {
  161. get
  162. {
  163. return this.reliableMessagingVersion;
  164. }
  165. set
  166. {
  167. if (value == null)
  168. {
  169. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  170. }
  171. if (!ReliableMessagingVersion.IsDefined(value))
  172. {
  173. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  174. }
  175. this.reliableMessagingVersion = value;
  176. }
  177. }
  178. static MessagePartSpecification BodyOnly
  179. {
  180. get
  181. {
  182. if (bodyOnly == null)
  183. {
  184. MessagePartSpecification temp = new MessagePartSpecification(true);
  185. temp.MakeReadOnly();
  186. bodyOnly = temp;
  187. }
  188. return bodyOnly;
  189. }
  190. }
  191. public override BindingElement Clone()
  192. {
  193. return new ReliableSessionBindingElement(this);
  194. }
  195. public override T GetProperty<T>(BindingContext context)
  196. {
  197. if (context == null)
  198. {
  199. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  200. }
  201. if (typeof(T) == typeof(ChannelProtectionRequirements))
  202. {
  203. ChannelProtectionRequirements myRequirements = this.GetProtectionRequirements();
  204. myRequirements.Add(context.GetInnerProperty<ChannelProtectionRequirements>() ?? new ChannelProtectionRequirements());
  205. return (T)(object)myRequirements;
  206. }
  207. else if (typeof(T) == typeof(IBindingDeliveryCapabilities))
  208. {
  209. return (T)(object)new BindingDeliveryCapabilitiesHelper(this, context.GetInnerProperty<IBindingDeliveryCapabilities>());
  210. }
  211. else
  212. {
  213. return context.GetInnerProperty<T>();
  214. }
  215. }
  216. ChannelProtectionRequirements GetProtectionRequirements()
  217. {
  218. // Listing headers that must be signed.
  219. ChannelProtectionRequirements result = new ChannelProtectionRequirements();
  220. MessagePartSpecification signedReliabilityMessageParts = WsrmIndex.GetSignedReliabilityMessageParts(
  221. this.reliableMessagingVersion);
  222. result.IncomingSignatureParts.AddParts(signedReliabilityMessageParts);
  223. result.OutgoingSignatureParts.AddParts(signedReliabilityMessageParts);
  224. if (this.reliableMessagingVersion == ReliableMessagingVersion.WSReliableMessagingFebruary2005)
  225. {
  226. // Adding RM protocol message actions so that each RM protocol message's body will be
  227. // signed and encrypted.
  228. // From the Client to the Service
  229. ScopedMessagePartSpecification signaturePart = result.IncomingSignatureParts;
  230. ScopedMessagePartSpecification encryptionPart = result.IncomingEncryptionParts;
  231. ProtectProtocolMessage(signaturePart, encryptionPart, WsrmFeb2005Strings.AckRequestedAction);
  232. ProtectProtocolMessage(signaturePart, encryptionPart, WsrmFeb2005Strings.CreateSequenceAction);
  233. ProtectProtocolMessage(signaturePart, encryptionPart, WsrmFeb2005Strings.SequenceAcknowledgementAction);
  234. ProtectProtocolMessage(signaturePart, encryptionPart, WsrmFeb2005Strings.LastMessageAction);
  235. ProtectProtocolMessage(signaturePart, encryptionPart, WsrmFeb2005Strings.TerminateSequenceAction);
  236. // From the Service to the Client
  237. signaturePart = result.OutgoingSignatureParts;
  238. encryptionPart = result.OutgoingEncryptionParts;
  239. ProtectProtocolMessage(signaturePart, encryptionPart, WsrmFeb2005Strings.CreateSequenceResponseAction);
  240. ProtectProtocolMessage(signaturePart, encryptionPart, WsrmFeb2005Strings.SequenceAcknowledgementAction);
  241. ProtectProtocolMessage(signaturePart, encryptionPart, WsrmFeb2005Strings.LastMessageAction);
  242. ProtectProtocolMessage(signaturePart, encryptionPart, WsrmFeb2005Strings.TerminateSequenceAction);
  243. }
  244. else if (this.reliableMessagingVersion == ReliableMessagingVersion.WSReliableMessaging11)
  245. {
  246. // Adding RM protocol message actions so that each RM protocol message's body will be
  247. // signed and encrypted.
  248. // From the Client to the Service
  249. ScopedMessagePartSpecification signaturePart = result.IncomingSignatureParts;
  250. ScopedMessagePartSpecification encryptionPart = result.IncomingEncryptionParts;
  251. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.AckRequestedAction);
  252. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.CloseSequenceAction);
  253. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.CloseSequenceResponseAction);
  254. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.CreateSequenceAction);
  255. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.FaultAction);
  256. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.SequenceAcknowledgementAction);
  257. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.TerminateSequenceAction);
  258. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.TerminateSequenceResponseAction);
  259. // From the Service to the Client
  260. signaturePart = result.OutgoingSignatureParts;
  261. encryptionPart = result.OutgoingEncryptionParts;
  262. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.AckRequestedAction);
  263. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.CloseSequenceAction);
  264. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.CloseSequenceResponseAction);
  265. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.CreateSequenceResponseAction);
  266. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.FaultAction);
  267. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.SequenceAcknowledgementAction);
  268. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.TerminateSequenceAction);
  269. ProtectProtocolMessage(signaturePart, encryptionPart, Wsrm11Strings.TerminateSequenceResponseAction);
  270. }
  271. else
  272. {
  273. throw Fx.AssertAndThrow("Reliable messaging version not supported.");
  274. }
  275. return result;
  276. }
  277. public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
  278. {
  279. if (context == null)
  280. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  281. this.VerifyTransportMode(context);
  282. this.SetSecuritySettings(context);
  283. InternalDuplexBindingElement.AddDuplexFactorySupport(context, ref this.internalDuplexBindingElement);
  284. if (typeof(TChannel) == typeof(IOutputSessionChannel))
  285. {
  286. if (context.CanBuildInnerChannelFactory<IRequestSessionChannel>())
  287. {
  288. return (IChannelFactory<TChannel>)(object)
  289. new ReliableChannelFactory<TChannel, IRequestSessionChannel>(
  290. this, context.BuildInnerChannelFactory<IRequestSessionChannel>(), context.Binding);
  291. }
  292. else if (context.CanBuildInnerChannelFactory<IRequestChannel>())
  293. {
  294. return (IChannelFactory<TChannel>)(object)
  295. new ReliableChannelFactory<TChannel, IRequestChannel>(
  296. this, context.BuildInnerChannelFactory<IRequestChannel>(), context.Binding);
  297. }
  298. else if (context.CanBuildInnerChannelFactory<IDuplexSessionChannel>())
  299. {
  300. return (IChannelFactory<TChannel>)(object)
  301. new ReliableChannelFactory<TChannel, IDuplexSessionChannel>(
  302. this, context.BuildInnerChannelFactory<IDuplexSessionChannel>(), context.Binding);
  303. }
  304. else if (context.CanBuildInnerChannelFactory<IDuplexChannel>())
  305. {
  306. return (IChannelFactory<TChannel>)(object)
  307. new ReliableChannelFactory<TChannel, IDuplexChannel>(
  308. this, context.BuildInnerChannelFactory<IDuplexChannel>(), context.Binding);
  309. }
  310. }
  311. else if (typeof(TChannel) == typeof(IDuplexSessionChannel))
  312. {
  313. if (context.CanBuildInnerChannelFactory<IDuplexSessionChannel>())
  314. {
  315. return (IChannelFactory<TChannel>)(object)
  316. new ReliableChannelFactory<TChannel, IDuplexSessionChannel>(
  317. this, context.BuildInnerChannelFactory<IDuplexSessionChannel>(), context.Binding);
  318. }
  319. else if (context.CanBuildInnerChannelFactory<IDuplexChannel>())
  320. {
  321. return (IChannelFactory<TChannel>)(object)
  322. new ReliableChannelFactory<TChannel, IDuplexChannel>(
  323. this, context.BuildInnerChannelFactory<IDuplexChannel>(), context.Binding);
  324. }
  325. }
  326. else if (typeof(TChannel) == typeof(IRequestSessionChannel))
  327. {
  328. if (context.CanBuildInnerChannelFactory<IRequestSessionChannel>())
  329. {
  330. return (IChannelFactory<TChannel>)(object)
  331. new ReliableChannelFactory<TChannel, IRequestSessionChannel>(
  332. this, context.BuildInnerChannelFactory<IRequestSessionChannel>(), context.Binding);
  333. }
  334. else if (context.CanBuildInnerChannelFactory<IRequestChannel>())
  335. {
  336. return (IChannelFactory<TChannel>)(object)
  337. new ReliableChannelFactory<TChannel, IRequestChannel>(
  338. this, context.BuildInnerChannelFactory<IRequestChannel>(), context.Binding);
  339. }
  340. }
  341. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("TChannel", SR.GetString(SR.ChannelTypeNotSupported, typeof(TChannel)));
  342. }
  343. public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
  344. {
  345. if (context == null)
  346. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  347. InternalDuplexBindingElement.AddDuplexFactorySupport(context, ref this.internalDuplexBindingElement);
  348. if (typeof(TChannel) == typeof(IOutputSessionChannel))
  349. {
  350. return context.CanBuildInnerChannelFactory<IRequestSessionChannel>()
  351. || context.CanBuildInnerChannelFactory<IRequestChannel>()
  352. || context.CanBuildInnerChannelFactory<IDuplexSessionChannel>()
  353. || context.CanBuildInnerChannelFactory<IDuplexChannel>();
  354. }
  355. else if (typeof(TChannel) == typeof(IDuplexSessionChannel))
  356. {
  357. return context.CanBuildInnerChannelFactory<IDuplexSessionChannel>()
  358. || context.CanBuildInnerChannelFactory<IDuplexChannel>();
  359. }
  360. else if (typeof(TChannel) == typeof(IRequestSessionChannel))
  361. {
  362. return context.CanBuildInnerChannelFactory<IRequestSessionChannel>()
  363. || context.CanBuildInnerChannelFactory<IRequestChannel>();
  364. }
  365. return false;
  366. }
  367. public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
  368. {
  369. if (context == null)
  370. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  371. this.VerifyTransportMode(context);
  372. this.SetSecuritySettings(context);
  373. #pragma warning suppress 56506 // BindingContext guarantees BindingParameters is never null.
  374. IMessageFilterTable<EndpointAddress> table = context.BindingParameters.Find<IMessageFilterTable<EndpointAddress>>();
  375. InternalDuplexBindingElement.AddDuplexListenerSupport(context, ref this.internalDuplexBindingElement);
  376. if (typeof(TChannel) == typeof(IInputSessionChannel))
  377. {
  378. ReliableChannelListenerBase<IInputSessionChannel> listener = null;
  379. if (context.CanBuildInnerChannelListener<IReplySessionChannel>())
  380. {
  381. listener = new ReliableInputListenerOverReplySession(this, context);
  382. }
  383. else if (context.CanBuildInnerChannelListener<IReplyChannel>())
  384. {
  385. listener = new ReliableInputListenerOverReply(this, context);
  386. }
  387. else if (context.CanBuildInnerChannelListener<IDuplexSessionChannel>())
  388. {
  389. listener = new ReliableInputListenerOverDuplexSession(this, context);
  390. }
  391. else if (context.CanBuildInnerChannelListener<IDuplexChannel>())
  392. {
  393. listener = new ReliableInputListenerOverDuplex(this, context);
  394. }
  395. if (listener != null)
  396. {
  397. listener.LocalAddresses = table;
  398. return (IChannelListener<TChannel>)(object)listener;
  399. }
  400. }
  401. else if (typeof(TChannel) == typeof(IDuplexSessionChannel))
  402. {
  403. ReliableChannelListenerBase<IDuplexSessionChannel> listener = null;
  404. if (context.CanBuildInnerChannelListener<IDuplexSessionChannel>())
  405. {
  406. listener = new ReliableDuplexListenerOverDuplexSession(this, context);
  407. }
  408. else if (context.CanBuildInnerChannelListener<IDuplexChannel>())
  409. {
  410. listener = new ReliableDuplexListenerOverDuplex(this, context);
  411. }
  412. if (listener != null)
  413. {
  414. listener.LocalAddresses = table;
  415. return (IChannelListener<TChannel>)(object)listener;
  416. }
  417. }
  418. else if (typeof(TChannel) == typeof(IReplySessionChannel))
  419. {
  420. ReliableChannelListenerBase<IReplySessionChannel> listener = null;
  421. if (context.CanBuildInnerChannelListener<IReplySessionChannel>())
  422. {
  423. listener = new ReliableReplyListenerOverReplySession(this, context);
  424. }
  425. else if (context.CanBuildInnerChannelListener<IReplyChannel>())
  426. {
  427. listener = new ReliableReplyListenerOverReply(this, context);
  428. }
  429. if (listener != null)
  430. {
  431. listener.LocalAddresses = table;
  432. return (IChannelListener<TChannel>)(object)listener;
  433. }
  434. }
  435. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("TChannel", SR.GetString(SR.ChannelTypeNotSupported, typeof(TChannel)));
  436. }
  437. public override bool CanBuildChannelListener<TChannel>(BindingContext context)
  438. {
  439. if (context == null)
  440. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  441. InternalDuplexBindingElement.AddDuplexListenerSupport(context, ref this.internalDuplexBindingElement);
  442. if (typeof(TChannel) == typeof(IInputSessionChannel))
  443. {
  444. return context.CanBuildInnerChannelListener<IReplySessionChannel>()
  445. || context.CanBuildInnerChannelListener<IReplyChannel>()
  446. || context.CanBuildInnerChannelListener<IDuplexSessionChannel>()
  447. || context.CanBuildInnerChannelListener<IDuplexChannel>();
  448. }
  449. else if (typeof(TChannel) == typeof(IDuplexSessionChannel))
  450. {
  451. return context.CanBuildInnerChannelListener<IDuplexSessionChannel>()
  452. || context.CanBuildInnerChannelListener<IDuplexChannel>();
  453. }
  454. else if (typeof(TChannel) == typeof(IReplySessionChannel))
  455. {
  456. return context.CanBuildInnerChannelListener<IReplySessionChannel>()
  457. || context.CanBuildInnerChannelListener<IReplyChannel>();
  458. }
  459. return false;
  460. }
  461. internal override bool IsMatch(BindingElement b)
  462. {
  463. if (b == null)
  464. return false;
  465. ReliableSessionBindingElement session = b as ReliableSessionBindingElement;
  466. if (session == null)
  467. return false;
  468. if (this.acknowledgementInterval != session.acknowledgementInterval)
  469. return false;
  470. if (this.flowControlEnabled != session.flowControlEnabled)
  471. return false;
  472. if (this.inactivityTimeout != session.inactivityTimeout)
  473. return false;
  474. if (this.maxPendingChannels != session.maxPendingChannels)
  475. return false;
  476. if (this.maxRetryCount != session.maxRetryCount)
  477. return false;
  478. if (this.maxTransferWindowSize != session.maxTransferWindowSize)
  479. return false;
  480. if (this.ordered != session.ordered)
  481. return false;
  482. if (this.reliableMessagingVersion != session.reliableMessagingVersion)
  483. return false;
  484. return true;
  485. }
  486. static void ProtectProtocolMessage(
  487. ScopedMessagePartSpecification signaturePart,
  488. ScopedMessagePartSpecification encryptionPart,
  489. string action)
  490. {
  491. signaturePart.AddParts(BodyOnly, action);
  492. encryptionPart.AddParts(MessagePartSpecification.NoParts, action);
  493. //encryptionPart.AddParts(BodyOnly, action);
  494. }
  495. void SetSecuritySettings(BindingContext context)
  496. {
  497. SecurityBindingElement element = context.RemainingBindingElements.Find<SecurityBindingElement>();
  498. if (element != null)
  499. {
  500. element.LocalServiceSettings.ReconnectTransportOnFailure = true;
  501. }
  502. }
  503. void VerifyTransportMode(BindingContext context)
  504. {
  505. TransportBindingElement transportElement = context.RemainingBindingElements.Find<TransportBindingElement>();
  506. // Verify ManualAdderssing is turned off.
  507. if ((transportElement != null) && (transportElement.ManualAddressing))
  508. {
  509. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  510. new InvalidOperationException(SR.GetString(SR.ManualAddressingNotSupported)));
  511. }
  512. ConnectionOrientedTransportBindingElement connectionElement = transportElement as ConnectionOrientedTransportBindingElement;
  513. HttpTransportBindingElement httpElement = transportElement as HttpTransportBindingElement;
  514. // Verify TransportMode is Buffered.
  515. TransferMode transportTransferMode;
  516. if (connectionElement != null)
  517. {
  518. transportTransferMode = connectionElement.TransferMode;
  519. }
  520. else if (httpElement != null)
  521. {
  522. transportTransferMode = httpElement.TransferMode;
  523. }
  524. else
  525. {
  526. // Not one of the elements we can check, we have to assume TransferMode.Buffered.
  527. transportTransferMode = TransferMode.Buffered;
  528. }
  529. if (transportTransferMode != TransferMode.Buffered)
  530. {
  531. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  532. new InvalidOperationException(SR.GetString(SR.TransferModeNotSupported,
  533. transportTransferMode, this.GetType().Name)));
  534. }
  535. }
  536. void IPolicyExportExtension.ExportPolicy(MetadataExporter exporter, PolicyConversionContext context)
  537. {
  538. if (exporter == null)
  539. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  540. if (context == null)
  541. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  542. if (context.BindingElements != null)
  543. {
  544. BindingElementCollection bindingElements = context.BindingElements;
  545. ReliableSessionBindingElement settings = bindingElements.Find<ReliableSessionBindingElement>();
  546. if (settings != null)
  547. {
  548. // ReliableSession assertion
  549. XmlElement assertion = settings.CreateReliabilityAssertion(exporter.PolicyVersion, bindingElements);
  550. context.GetBindingAssertions().Add(assertion);
  551. }
  552. }
  553. }
  554. static XmlElement CreatePolicyElement(PolicyVersion policyVersion, XmlDocument doc)
  555. {
  556. string policy = MetadataStrings.WSPolicy.Elements.Policy;
  557. string policyNs = policyVersion.Namespace;
  558. string policyPrefix = MetadataStrings.WSPolicy.Prefix;
  559. return doc.CreateElement(policyPrefix, policy, policyNs);
  560. }
  561. XmlElement CreateReliabilityAssertion(PolicyVersion policyVersion, BindingElementCollection bindingElements)
  562. {
  563. XmlDocument doc = new XmlDocument();
  564. XmlElement child = null;
  565. string reliableSessionPrefix;
  566. string reliableSessionNs;
  567. string assertionPrefix;
  568. string assertionNs;
  569. if (this.ReliableMessagingVersion == ReliableMessagingVersion.WSReliableMessagingFebruary2005)
  570. {
  571. reliableSessionPrefix = ReliableSessionPolicyStrings.ReliableSessionFebruary2005Prefix;
  572. reliableSessionNs = ReliableSessionPolicyStrings.ReliableSessionFebruary2005Namespace;
  573. assertionPrefix = reliableSessionPrefix;
  574. assertionNs = reliableSessionNs;
  575. }
  576. else
  577. {
  578. reliableSessionPrefix = ReliableSessionPolicyStrings.ReliableSession11Prefix;
  579. reliableSessionNs = ReliableSessionPolicyStrings.ReliableSession11Namespace;
  580. assertionPrefix = ReliableSessionPolicyStrings.NET11Prefix;
  581. assertionNs = ReliableSessionPolicyStrings.NET11Namespace;
  582. }
  583. // ReliableSession assertion
  584. XmlElement assertion = doc.CreateElement(reliableSessionPrefix, ReliableSessionPolicyStrings.ReliableSessionName, reliableSessionNs);
  585. if (this.ReliableMessagingVersion == ReliableMessagingVersion.WSReliableMessaging11)
  586. {
  587. // Policy
  588. XmlElement policy = CreatePolicyElement(policyVersion, doc);
  589. // SequenceSTR
  590. if (IsSecureConversationEnabled(bindingElements))
  591. {
  592. XmlElement sequenceSTR = doc.CreateElement(reliableSessionPrefix, ReliableSessionPolicyStrings.SequenceSTR, reliableSessionNs);
  593. policy.AppendChild(sequenceSTR);
  594. }
  595. // DeliveryAssurance
  596. XmlElement deliveryAssurance = doc.CreateElement(reliableSessionPrefix, ReliableSessionPolicyStrings.DeliveryAssurance, reliableSessionNs);
  597. // Policy
  598. XmlElement nestedPolicy = CreatePolicyElement(policyVersion, doc);
  599. // ExactlyOnce
  600. XmlElement exactlyOnce = doc.CreateElement(reliableSessionPrefix, ReliableSessionPolicyStrings.ExactlyOnce, reliableSessionNs);
  601. nestedPolicy.AppendChild(exactlyOnce);
  602. if (this.ordered)
  603. {
  604. // InOrder
  605. XmlElement inOrder = doc.CreateElement(reliableSessionPrefix, ReliableSessionPolicyStrings.InOrder, reliableSessionNs);
  606. nestedPolicy.AppendChild(inOrder);
  607. }
  608. deliveryAssurance.AppendChild(nestedPolicy);
  609. policy.AppendChild(deliveryAssurance);
  610. assertion.AppendChild(policy);
  611. }
  612. // Nested InactivityTimeout assertion
  613. child = doc.CreateElement(assertionPrefix, ReliableSessionPolicyStrings.InactivityTimeout, assertionNs);
  614. WriteMillisecondsAttribute(child, this.InactivityTimeout);
  615. assertion.AppendChild(child);
  616. // Nested AcknowledgementInterval assertion
  617. child = doc.CreateElement(assertionPrefix, ReliableSessionPolicyStrings.AcknowledgementInterval, assertionNs);
  618. WriteMillisecondsAttribute(child, this.AcknowledgementInterval);
  619. assertion.AppendChild(child);
  620. return assertion;
  621. }
  622. static bool IsSecureConversationEnabled(BindingElementCollection bindingElements)
  623. {
  624. bool foundRM = false;
  625. for (int i = 0; i < bindingElements.Count; i++)
  626. {
  627. if (!foundRM)
  628. {
  629. ReliableSessionBindingElement bindingElement = bindingElements[i] as ReliableSessionBindingElement;
  630. foundRM = (bindingElement != null);
  631. }
  632. else
  633. {
  634. SecurityBindingElement securityBindingElement = bindingElements[i] as SecurityBindingElement;
  635. if (securityBindingElement != null)
  636. {
  637. SecurityBindingElement bootstrapSecurity;
  638. // The difference in bool (requireCancellation) does not affect whether the binding is valid,
  639. // but the method does match on the value so we need to pass both true and false.
  640. return SecurityBindingElement.IsSecureConversationBinding(securityBindingElement, true, out bootstrapSecurity)
  641. || SecurityBindingElement.IsSecureConversationBinding(securityBindingElement, false, out bootstrapSecurity);
  642. }
  643. break;
  644. }
  645. }
  646. return false;
  647. }
  648. static void WriteMillisecondsAttribute(XmlElement childElement, TimeSpan timeSpan)
  649. {
  650. UInt64 milliseconds = Convert.ToUInt64(timeSpan.TotalMilliseconds);
  651. childElement.SetAttribute(ReliableSessionPolicyStrings.Milliseconds, XmlConvert.ToString(milliseconds));
  652. }
  653. class BindingDeliveryCapabilitiesHelper : IBindingDeliveryCapabilities
  654. {
  655. ReliableSessionBindingElement element;
  656. IBindingDeliveryCapabilities inner;
  657. internal BindingDeliveryCapabilitiesHelper(ReliableSessionBindingElement element, IBindingDeliveryCapabilities inner)
  658. {
  659. this.element = element;
  660. this.inner = inner;
  661. }
  662. bool IBindingDeliveryCapabilities.AssuresOrderedDelivery
  663. {
  664. get { return element.Ordered; }
  665. }
  666. bool IBindingDeliveryCapabilities.QueuedDelivery
  667. {
  668. get { return inner != null ? inner.QueuedDelivery : false; }
  669. }
  670. }
  671. }
  672. }