TransactionChannelFactory.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Collections.Generic;
  7. using System.Runtime;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Description;
  10. using System.ServiceModel.Security;
  11. using SR = System.ServiceModel.SR;
  12. sealed class TransactionChannelFactory<TChannel> : LayeredChannelFactory<TChannel>, ITransactionChannelManager
  13. {
  14. TransactionFlowOption flowIssuedTokens;
  15. SecurityStandardsManager standardsManager;
  16. Dictionary<DirectionalAction, TransactionFlowOption> dictionary;
  17. TransactionProtocol transactionProtocol;
  18. bool allowWildcardAction;
  19. public TransactionChannelFactory(
  20. TransactionProtocol transactionProtocol,
  21. BindingContext context,
  22. Dictionary<DirectionalAction, TransactionFlowOption> dictionary,
  23. bool allowWildcardAction)
  24. : base(context.Binding, context.BuildInnerChannelFactory<TChannel>())
  25. {
  26. this.dictionary = dictionary;
  27. this.TransactionProtocol = transactionProtocol;
  28. this.allowWildcardAction = allowWildcardAction;
  29. this.standardsManager = SecurityStandardsHelper.CreateStandardsManager(this.TransactionProtocol);
  30. }
  31. public TransactionProtocol TransactionProtocol
  32. {
  33. get
  34. {
  35. return this.transactionProtocol;
  36. }
  37. set
  38. {
  39. if (!TransactionProtocol.IsDefined(value))
  40. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  41. new ArgumentException(SR.GetString(SR.SFxBadTransactionProtocols)));
  42. this.transactionProtocol = value;
  43. }
  44. }
  45. public TransactionFlowOption FlowIssuedTokens
  46. {
  47. get { return this.flowIssuedTokens; }
  48. set { this.flowIssuedTokens = value; }
  49. }
  50. public SecurityStandardsManager StandardsManager
  51. {
  52. get { return this.standardsManager; }
  53. set
  54. {
  55. this.standardsManager = (value != null ? value : SecurityStandardsHelper.CreateStandardsManager(this.transactionProtocol));
  56. }
  57. }
  58. public IDictionary<DirectionalAction, TransactionFlowOption> Dictionary
  59. {
  60. get { return this.dictionary; }
  61. }
  62. public TransactionFlowOption GetTransaction(MessageDirection direction, string action)
  63. {
  64. TransactionFlowOption txOption;
  65. if (!dictionary.TryGetValue(new DirectionalAction(direction, action), out txOption))
  66. {
  67. //Fixinng this for clients that opted in for lesser validation before flowing out a transaction
  68. if (this.allowWildcardAction && dictionary.TryGetValue(new DirectionalAction(direction, MessageHeaders.WildcardAction), out txOption))
  69. {
  70. return txOption;
  71. }
  72. else
  73. return TransactionFlowOption.NotAllowed;
  74. }
  75. else
  76. return txOption;
  77. }
  78. protected override TChannel OnCreateChannel(EndpointAddress remoteAddress, Uri via)
  79. {
  80. TChannel innerChannel = ((IChannelFactory<TChannel>)InnerChannelFactory).CreateChannel(remoteAddress, via);
  81. return CreateTransactionChannel(innerChannel);
  82. }
  83. TChannel CreateTransactionChannel(TChannel innerChannel)
  84. {
  85. if (typeof(TChannel) == typeof(IDuplexSessionChannel))
  86. {
  87. return (TChannel)(object)new TransactionDuplexSessionChannel(this, (IDuplexSessionChannel)(object)innerChannel);
  88. }
  89. else if (typeof(TChannel) == typeof(IRequestSessionChannel))
  90. {
  91. return (TChannel)(object)new TransactionRequestSessionChannel(this, (IRequestSessionChannel)(object)innerChannel);
  92. }
  93. else if (typeof(TChannel) == typeof(IOutputSessionChannel))
  94. {
  95. return (TChannel)(object)new TransactionOutputSessionChannel(this, (IOutputSessionChannel)(object)innerChannel);
  96. }
  97. else if (typeof(TChannel) == typeof(IOutputChannel))
  98. {
  99. return (TChannel)(object)new TransactionOutputChannel(this, (IOutputChannel)(object)innerChannel);
  100. }
  101. else if (typeof(TChannel) == typeof(IRequestChannel))
  102. {
  103. return (TChannel)(object)new TransactionRequestChannel(this, (IRequestChannel)(object)innerChannel);
  104. }
  105. else if (typeof(TChannel) == typeof(IDuplexChannel))
  106. {
  107. return (TChannel)(object)new TransactionDuplexChannel(this, (IDuplexChannel)(object)innerChannel);
  108. }
  109. else
  110. {
  111. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateChannelTypeNotSupportedException(typeof(TChannel)));
  112. }
  113. }
  114. //===========================================================
  115. // Transaction Output Channel classes
  116. //===========================================================
  117. sealed class TransactionOutputChannel : TransactionOutputChannelGeneric<IOutputChannel>
  118. {
  119. public TransactionOutputChannel(ChannelManagerBase channelManager, IOutputChannel innerChannel)
  120. : base(channelManager, innerChannel)
  121. {
  122. }
  123. }
  124. sealed class TransactionRequestChannel : TransactionRequestChannelGeneric<IRequestChannel>
  125. {
  126. public TransactionRequestChannel(ChannelManagerBase channelManager, IRequestChannel innerChannel)
  127. : base(channelManager, innerChannel)
  128. {
  129. }
  130. }
  131. sealed class TransactionDuplexChannel : TransactionOutputDuplexChannelGeneric<IDuplexChannel>
  132. {
  133. public TransactionDuplexChannel(ChannelManagerBase channelManager, IDuplexChannel innerChannel)
  134. : base(channelManager, innerChannel)
  135. {
  136. }
  137. }
  138. sealed class TransactionOutputSessionChannel : TransactionOutputChannelGeneric<IOutputSessionChannel>, IOutputSessionChannel
  139. {
  140. public TransactionOutputSessionChannel(ChannelManagerBase channelManager, IOutputSessionChannel innerChannel)
  141. : base(channelManager, innerChannel)
  142. {
  143. }
  144. public IOutputSession Session { get { return InnerChannel.Session; } }
  145. }
  146. sealed class TransactionRequestSessionChannel : TransactionRequestChannelGeneric<IRequestSessionChannel>, IRequestSessionChannel
  147. {
  148. public TransactionRequestSessionChannel(ChannelManagerBase channelManager, IRequestSessionChannel innerChannel)
  149. : base(channelManager, innerChannel)
  150. {
  151. }
  152. public IOutputSession Session { get { return InnerChannel.Session; } }
  153. }
  154. sealed class TransactionDuplexSessionChannel : TransactionOutputDuplexChannelGeneric<IDuplexSessionChannel>, IDuplexSessionChannel
  155. {
  156. public TransactionDuplexSessionChannel(ChannelManagerBase channelManager, IDuplexSessionChannel innerChannel)
  157. : base(channelManager, innerChannel)
  158. {
  159. }
  160. public IDuplexSession Session { get { return InnerChannel.Session; } }
  161. }
  162. }
  163. static class SecurityStandardsHelper
  164. {
  165. static SecurityStandardsManager SecurityStandardsManager2007 =
  166. CreateStandardsManager(MessageSecurityVersion.WSSecurity11WSTrust13WSSecureConversation13WSSecurityPolicy12);
  167. static SecurityStandardsManager CreateStandardsManager(MessageSecurityVersion securityVersion)
  168. {
  169. return new SecurityStandardsManager(
  170. securityVersion,
  171. new WSSecurityTokenSerializer(securityVersion.SecurityVersion, securityVersion.TrustVersion, securityVersion.SecureConversationVersion, false, null, null, null));
  172. }
  173. public static SecurityStandardsManager CreateStandardsManager(TransactionProtocol transactionProtocol)
  174. {
  175. if (transactionProtocol == TransactionProtocol.WSAtomicTransactionOctober2004 ||
  176. transactionProtocol == TransactionProtocol.OleTransactions)
  177. {
  178. return SecurityStandardsManager.DefaultInstance;
  179. }
  180. else
  181. {
  182. return SecurityStandardsHelper.SecurityStandardsManager2007;
  183. }
  184. }
  185. }
  186. //==============================================================
  187. // Transaction channel base generic classes
  188. //==============================================================
  189. class TransactionOutputChannelGeneric<TChannel> : TransactionChannel<TChannel>, IOutputChannel
  190. where TChannel : class, IOutputChannel
  191. {
  192. public TransactionOutputChannelGeneric(ChannelManagerBase channelManager, TChannel innerChannel)
  193. : base(channelManager, innerChannel)
  194. {
  195. }
  196. public EndpointAddress RemoteAddress
  197. {
  198. get
  199. {
  200. return InnerChannel.RemoteAddress;
  201. }
  202. }
  203. public Uri Via
  204. {
  205. get
  206. {
  207. return InnerChannel.Via;
  208. }
  209. }
  210. public IAsyncResult BeginSend(Message message, AsyncCallback callback, object state)
  211. {
  212. return this.BeginSend(message, this.DefaultSendTimeout, callback, state);
  213. }
  214. public IAsyncResult BeginSend(Message message, TimeSpan timeout, AsyncCallback asyncCallback, object state)
  215. {
  216. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  217. WriteTransactionDataToMessage(message, MessageDirection.Input);
  218. return InnerChannel.BeginSend(message, timeoutHelper.RemainingTime(), asyncCallback, state);
  219. }
  220. public void EndSend(IAsyncResult result)
  221. {
  222. InnerChannel.EndSend(result);
  223. }
  224. public void Send(Message message)
  225. {
  226. this.Send(message, this.DefaultSendTimeout);
  227. }
  228. public void Send(Message message, TimeSpan timeout)
  229. {
  230. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  231. WriteTransactionDataToMessage(message, MessageDirection.Input);
  232. InnerChannel.Send(message, timeoutHelper.RemainingTime());
  233. }
  234. }
  235. class TransactionRequestChannelGeneric<TChannel> : TransactionChannel<TChannel>, IRequestChannel
  236. where TChannel : class, IRequestChannel
  237. {
  238. public TransactionRequestChannelGeneric(ChannelManagerBase channelManager, TChannel innerChannel)
  239. : base(channelManager, innerChannel)
  240. {
  241. }
  242. public EndpointAddress RemoteAddress
  243. {
  244. get
  245. {
  246. return InnerChannel.RemoteAddress;
  247. }
  248. }
  249. public Uri Via
  250. {
  251. get
  252. {
  253. return InnerChannel.Via;
  254. }
  255. }
  256. public IAsyncResult BeginRequest(Message message, AsyncCallback callback, object state)
  257. {
  258. return this.BeginRequest(message, this.DefaultSendTimeout, callback, state);
  259. }
  260. public IAsyncResult BeginRequest(Message message, TimeSpan timeout, AsyncCallback asyncCallback, object state)
  261. {
  262. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  263. WriteTransactionDataToMessage(message, MessageDirection.Input);
  264. return InnerChannel.BeginRequest(message, timeoutHelper.RemainingTime(), asyncCallback, state);
  265. }
  266. public Message EndRequest(IAsyncResult result)
  267. {
  268. Message reply = InnerChannel.EndRequest(result);
  269. if (reply != null)
  270. this.ReadIssuedTokens(reply, MessageDirection.Output);
  271. return reply;
  272. }
  273. public Message Request(Message message)
  274. {
  275. return this.Request(message, this.DefaultSendTimeout);
  276. }
  277. public Message Request(Message message, TimeSpan timeout)
  278. {
  279. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  280. WriteTransactionDataToMessage(message, MessageDirection.Input);
  281. Message reply = InnerChannel.Request(message, timeoutHelper.RemainingTime());
  282. if (reply != null)
  283. this.ReadIssuedTokens(reply, MessageDirection.Output);
  284. return reply;
  285. }
  286. }
  287. class TransactionOutputDuplexChannelGeneric<TChannel> : TransactionDuplexChannelGeneric<TChannel>
  288. where TChannel : class, IDuplexChannel
  289. {
  290. public TransactionOutputDuplexChannelGeneric(ChannelManagerBase channelManager, TChannel innerChannel)
  291. : base(channelManager, innerChannel, MessageDirection.Output)
  292. {
  293. }
  294. }
  295. }