TransactionFlowBindingElement.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System;
  7. using System.ServiceModel.Description;
  8. using System.Collections.Generic;
  9. using System.Runtime.Serialization;
  10. using System.ServiceModel;
  11. using System.ServiceModel.Security;
  12. using System.ComponentModel;
  13. using System.ServiceModel.Transactions;
  14. using System.Xml;
  15. public sealed class TransactionFlowBindingElement : BindingElement, IPolicyExportExtension
  16. {
  17. bool transactions;
  18. TransactionFlowOption issuedTokens;
  19. TransactionProtocol transactionProtocol;
  20. public TransactionFlowBindingElement()
  21. : this(true, TransactionFlowDefaults.TransactionProtocol)
  22. {
  23. }
  24. public TransactionFlowBindingElement(TransactionProtocol transactionProtocol)
  25. : this(true, transactionProtocol)
  26. {
  27. }
  28. internal TransactionFlowBindingElement(bool transactions)
  29. : this(transactions, TransactionFlowDefaults.TransactionProtocol)
  30. {
  31. }
  32. internal TransactionFlowBindingElement(bool transactions, TransactionProtocol transactionProtocol)
  33. {
  34. this.transactions = transactions;
  35. this.issuedTokens = transactions ? TransactionFlowOption.Allowed : TransactionFlowOption.NotAllowed;
  36. if (!TransactionProtocol.IsDefined(transactionProtocol))
  37. {
  38. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.ConfigInvalidTransactionFlowProtocolValue, transactionProtocol.ToString()));
  39. }
  40. this.transactionProtocol = transactionProtocol;
  41. }
  42. TransactionFlowBindingElement(TransactionFlowBindingElement elementToBeCloned)
  43. : base(elementToBeCloned)
  44. {
  45. this.transactions = elementToBeCloned.transactions;
  46. this.issuedTokens = elementToBeCloned.issuedTokens;
  47. if (!TransactionProtocol.IsDefined(elementToBeCloned.transactionProtocol))
  48. {
  49. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.ConfigInvalidTransactionFlowProtocolValue, elementToBeCloned.transactionProtocol.ToString()));
  50. }
  51. this.transactionProtocol = elementToBeCloned.transactionProtocol;
  52. this.AllowWildcardAction = elementToBeCloned.AllowWildcardAction;
  53. }
  54. internal bool Transactions
  55. {
  56. get
  57. {
  58. return this.transactions;
  59. }
  60. set
  61. {
  62. this.transactions = value;
  63. this.issuedTokens = value ? TransactionFlowOption.Allowed : TransactionFlowOption.NotAllowed;
  64. }
  65. }
  66. internal TransactionFlowOption IssuedTokens
  67. {
  68. get
  69. {
  70. return this.issuedTokens;
  71. }
  72. set
  73. {
  74. ValidateOption(value);
  75. this.issuedTokens = value;
  76. }
  77. }
  78. public override BindingElement Clone()
  79. {
  80. return new TransactionFlowBindingElement(this);
  81. }
  82. bool IsFlowEnabled(Dictionary<DirectionalAction, TransactionFlowOption> dictionary)
  83. {
  84. if (this.issuedTokens != TransactionFlowOption.NotAllowed)
  85. {
  86. return true;
  87. }
  88. if (!this.transactions)
  89. {
  90. return false;
  91. }
  92. foreach (TransactionFlowOption option in dictionary.Values)
  93. {
  94. if (option != TransactionFlowOption.NotAllowed)
  95. {
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. internal bool IsFlowEnabled(ContractDescription contract)
  102. {
  103. if (this.issuedTokens != TransactionFlowOption.NotAllowed)
  104. {
  105. return true;
  106. }
  107. if (!this.transactions)
  108. {
  109. return false;
  110. }
  111. foreach (OperationDescription operation in contract.Operations)
  112. {
  113. TransactionFlowAttribute parameter = operation.Behaviors.Find<TransactionFlowAttribute>();
  114. if (parameter != null)
  115. {
  116. if (parameter.Transactions != TransactionFlowOption.NotAllowed)
  117. {
  118. return true;
  119. }
  120. }
  121. }
  122. return false;
  123. }
  124. public TransactionProtocol TransactionProtocol
  125. {
  126. get
  127. {
  128. return this.transactionProtocol;
  129. }
  130. set
  131. {
  132. if (!TransactionProtocol.IsDefined(value))
  133. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  134. this.transactionProtocol = value;
  135. }
  136. }
  137. [DefaultValue(false)]
  138. public bool AllowWildcardAction
  139. {
  140. get;
  141. set;
  142. }
  143. internal static void ValidateOption(TransactionFlowOption opt)
  144. {
  145. if (!TransactionFlowOptionHelper.IsDefined(opt))
  146. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.TransactionFlowBadOption)));
  147. }
  148. [EditorBrowsable(EditorBrowsableState.Never)]
  149. public bool ShouldSerializeTransactionProtocol()
  150. {
  151. return this.TransactionProtocol != TransactionProtocol.Default;
  152. }
  153. public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
  154. {
  155. if (context == null)
  156. {
  157. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("context"));
  158. }
  159. if (typeof(TChannel) == typeof(IOutputChannel)
  160. || typeof(TChannel) == typeof(IDuplexChannel)
  161. || typeof(TChannel) == typeof(IRequestChannel)
  162. || typeof(TChannel) == typeof(IOutputSessionChannel)
  163. || typeof(TChannel) == typeof(IRequestSessionChannel)
  164. || typeof(TChannel) == typeof(IDuplexSessionChannel))
  165. {
  166. return context.CanBuildInnerChannelFactory<TChannel>();
  167. }
  168. return false;
  169. }
  170. // The BuildChannelFactory and BuildListenerFactory methods looks for this BindingParameter
  171. // in the BindingContext:
  172. // - Dictionary<DirectionalAction, TransactionFlowOption>
  173. // which has the per-operation TransactionFlowOptions
  174. public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
  175. {
  176. if (context == null)
  177. {
  178. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  179. }
  180. if (!this.CanBuildChannelFactory<TChannel>(context))
  181. {
  182. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("TChannel", SR.GetString(SR.ChannelTypeNotSupported, typeof(TChannel)));
  183. }
  184. Dictionary<DirectionalAction, TransactionFlowOption> dictionary = GetDictionary(context);
  185. if (!this.IsFlowEnabled(dictionary))
  186. {
  187. return context.BuildInnerChannelFactory<TChannel>();
  188. }
  189. if (this.issuedTokens == TransactionFlowOption.NotAllowed)
  190. {
  191. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.TransactionFlowRequiredIssuedTokens)));
  192. }
  193. TransactionChannelFactory<TChannel> channelFactory =
  194. new TransactionChannelFactory<TChannel>(this.transactionProtocol, context, dictionary, this.AllowWildcardAction);
  195. channelFactory.FlowIssuedTokens = this.IssuedTokens;
  196. return channelFactory;
  197. }
  198. public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
  199. {
  200. if (context == null)
  201. {
  202. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("context"));
  203. }
  204. if (!context.CanBuildInnerChannelListener<TChannel>())
  205. {
  206. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("TChannel", SR.GetString(SR.ChannelTypeNotSupported, typeof(TChannel)));
  207. }
  208. Dictionary<DirectionalAction, TransactionFlowOption> dictionary = GetDictionary(context);
  209. if (!this.IsFlowEnabled(dictionary))
  210. {
  211. return context.BuildInnerChannelListener<TChannel>();
  212. }
  213. if (this.issuedTokens == TransactionFlowOption.NotAllowed)
  214. {
  215. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.TransactionFlowRequiredIssuedTokens)));
  216. }
  217. IChannelListener<TChannel> innerListener = context.BuildInnerChannelListener<TChannel>();
  218. TransactionChannelListener<TChannel> listener = new TransactionChannelListener<TChannel>(this.transactionProtocol, context.Binding, dictionary, innerListener);
  219. listener.FlowIssuedTokens = this.IssuedTokens;
  220. return listener;
  221. }
  222. public override bool CanBuildChannelListener<TChannel>(BindingContext context)
  223. {
  224. if (!context.CanBuildInnerChannelListener<TChannel>())
  225. return false;
  226. return (typeof(TChannel) == typeof(IInputChannel) ||
  227. typeof(TChannel) == typeof(IReplyChannel) ||
  228. typeof(TChannel) == typeof(IDuplexChannel) ||
  229. typeof(TChannel) == typeof(IInputSessionChannel) ||
  230. typeof(TChannel) == typeof(IReplySessionChannel) ||
  231. typeof(TChannel) == typeof(IDuplexSessionChannel));
  232. }
  233. Dictionary<DirectionalAction, TransactionFlowOption> GetDictionary(BindingContext context)
  234. {
  235. Dictionary<DirectionalAction, TransactionFlowOption> dictionary =
  236. context.BindingParameters.Find<Dictionary<DirectionalAction, TransactionFlowOption>>();
  237. if (dictionary == null)
  238. dictionary = new Dictionary<DirectionalAction, TransactionFlowOption>();
  239. return dictionary;
  240. }
  241. internal static MessagePartSpecification GetIssuedTokenHeaderSpecification(SecurityStandardsManager standardsManager)
  242. {
  243. MessagePartSpecification result;
  244. if (standardsManager.TrustDriver.IsIssuedTokensSupported)
  245. result = new MessagePartSpecification(new XmlQualifiedName(standardsManager.TrustDriver.IssuedTokensHeaderName, standardsManager.TrustDriver.IssuedTokensHeaderNamespace));
  246. else
  247. {
  248. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.TrustDriverVersionDoesNotSupportIssuedTokens)));
  249. }
  250. return result;
  251. }
  252. public override T GetProperty<T>(BindingContext context)
  253. {
  254. if (typeof(T) == typeof(ChannelProtectionRequirements))
  255. {
  256. ChannelProtectionRequirements myRequirements = this.GetProtectionRequirements();
  257. if (myRequirements != null)
  258. {
  259. myRequirements.Add(context.GetInnerProperty<ChannelProtectionRequirements>() ?? new ChannelProtectionRequirements());
  260. return (T)(object)myRequirements;
  261. }
  262. else
  263. {
  264. return (T)(object)context.GetInnerProperty<ChannelProtectionRequirements>();
  265. }
  266. }
  267. else
  268. {
  269. return context.GetInnerProperty<T>();
  270. }
  271. }
  272. ChannelProtectionRequirements GetProtectionRequirements()
  273. {
  274. if (this.Transactions || (this.IssuedTokens != TransactionFlowOption.NotAllowed))
  275. {
  276. ChannelProtectionRequirements requirements = new ChannelProtectionRequirements();
  277. if (this.Transactions)
  278. {
  279. MessagePartSpecification p = new MessagePartSpecification(
  280. new XmlQualifiedName(CoordinationExternalStrings.CoordinationContext, CoordinationExternal10Strings.Namespace),
  281. new XmlQualifiedName(CoordinationExternalStrings.CoordinationContext, CoordinationExternal11Strings.Namespace),
  282. new XmlQualifiedName(OleTxTransactionExternalStrings.OleTxTransaction, OleTxTransactionExternalStrings.Namespace));
  283. p.MakeReadOnly();
  284. requirements.IncomingSignatureParts.AddParts(p);
  285. requirements.OutgoingSignatureParts.AddParts(p);
  286. requirements.IncomingEncryptionParts.AddParts(p);
  287. requirements.OutgoingEncryptionParts.AddParts(p);
  288. }
  289. if (this.IssuedTokens != TransactionFlowOption.NotAllowed)
  290. {
  291. MessagePartSpecification trustParts = GetIssuedTokenHeaderSpecification(SecurityStandardsManager.DefaultInstance);
  292. trustParts.MakeReadOnly();
  293. requirements.IncomingSignatureParts.AddParts(trustParts);
  294. requirements.IncomingEncryptionParts.AddParts(trustParts);
  295. requirements.OutgoingSignatureParts.AddParts(trustParts);
  296. requirements.OutgoingEncryptionParts.AddParts(trustParts);
  297. }
  298. MessagePartSpecification body = new MessagePartSpecification(true);
  299. body.MakeReadOnly();
  300. requirements.OutgoingSignatureParts.AddParts(body, FaultCodeConstants.Actions.Transactions);
  301. requirements.OutgoingEncryptionParts.AddParts(body, FaultCodeConstants.Actions.Transactions);
  302. return requirements;
  303. }
  304. else
  305. {
  306. return null;
  307. }
  308. }
  309. XmlElement GetAssertion(XmlDocument doc, TransactionFlowOption option, string prefix, string name, string ns, string policyNs)
  310. {
  311. if (doc == null)
  312. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("doc");
  313. XmlElement result = null;
  314. switch (option)
  315. {
  316. case TransactionFlowOption.NotAllowed:
  317. // Don't generate an assertion
  318. break;
  319. case TransactionFlowOption.Allowed:
  320. result = doc.CreateElement(prefix, name, ns);
  321. // Always insert the real wsp:Optional attribute
  322. XmlAttribute attr = doc.CreateAttribute(TransactionPolicyStrings.OptionalPrefix11,
  323. TransactionPolicyStrings.OptionalLocal, policyNs);
  324. attr.Value = TransactionPolicyStrings.TrueValue;
  325. result.Attributes.Append(attr);
  326. // For legacy protocols, also insert the legacy attribute for backward compat
  327. if (this.transactionProtocol == TransactionProtocol.OleTransactions ||
  328. this.transactionProtocol == TransactionProtocol.WSAtomicTransactionOctober2004)
  329. {
  330. XmlAttribute attrLegacy = doc.CreateAttribute(TransactionPolicyStrings.OptionalPrefix10,
  331. TransactionPolicyStrings.OptionalLocal, TransactionPolicyStrings.OptionalNamespaceLegacy);
  332. attrLegacy.Value = TransactionPolicyStrings.TrueValue;
  333. result.Attributes.Append(attrLegacy);
  334. }
  335. break;
  336. case TransactionFlowOption.Mandatory:
  337. result = doc.CreateElement(prefix, name, ns);
  338. break;
  339. }
  340. return result;
  341. }
  342. void IPolicyExportExtension.ExportPolicy(MetadataExporter exporter, PolicyConversionContext context)
  343. {
  344. if (exporter == null)
  345. {
  346. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("exporter");
  347. }
  348. if (context == null)
  349. {
  350. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  351. }
  352. TransactionFlowBindingElement bindingElement = context.BindingElements.Find<TransactionFlowBindingElement>();
  353. if (bindingElement == null || !bindingElement.Transactions)
  354. return;
  355. XmlDocument doc = new XmlDocument();
  356. XmlElement assertion = null;
  357. foreach (OperationDescription operation in context.Contract.Operations)
  358. {
  359. TransactionFlowAttribute contextParam = operation.Behaviors.Find<TransactionFlowAttribute>();
  360. TransactionFlowOption txFlowOption = contextParam == null ? TransactionFlowOption.NotAllowed : contextParam.Transactions;
  361. // Transactions
  362. if (bindingElement.TransactionProtocol == TransactionProtocol.OleTransactions)
  363. {
  364. assertion = GetAssertion(doc, txFlowOption,
  365. TransactionPolicyStrings.OleTxTransactionsPrefix, TransactionPolicyStrings.OleTxTransactionsLocal,
  366. TransactionPolicyStrings.OleTxTransactionsNamespace, exporter.PolicyVersion.Namespace);
  367. }
  368. else if (bindingElement.TransactionProtocol == TransactionProtocol.WSAtomicTransactionOctober2004)
  369. {
  370. assertion = GetAssertion(doc, txFlowOption,
  371. TransactionPolicyStrings.WsatTransactionsPrefix, TransactionPolicyStrings.WsatTransactionsLocal,
  372. TransactionPolicyStrings.WsatTransactionsNamespace10, exporter.PolicyVersion.Namespace);
  373. }
  374. else if (bindingElement.TransactionProtocol == TransactionProtocol.WSAtomicTransaction11)
  375. {
  376. assertion = GetAssertion(doc, txFlowOption,
  377. TransactionPolicyStrings.WsatTransactionsPrefix, TransactionPolicyStrings.WsatTransactionsLocal,
  378. TransactionPolicyStrings.WsatTransactionsNamespace11, exporter.PolicyVersion.Namespace);
  379. }
  380. if (assertion != null)
  381. context.GetOperationBindingAssertions(operation).Add(assertion);
  382. }
  383. }
  384. internal override bool IsMatch(BindingElement b)
  385. {
  386. if (b == null)
  387. return false;
  388. TransactionFlowBindingElement txFlow = b as TransactionFlowBindingElement;
  389. if (txFlow == null)
  390. return false;
  391. if (this.transactions != txFlow.transactions)
  392. return false;
  393. if (this.issuedTokens != txFlow.issuedTokens)
  394. return false;
  395. if (this.transactionProtocol != txFlow.transactionProtocol)
  396. return false;
  397. return true;
  398. }
  399. }
  400. }