TransactionFlowBindingElement.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // TransactionFlowBindingElement.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.ServiceModel.Description;
  31. using System.ServiceModel.Security;
  32. using System.ServiceModel.Channels;
  33. using System.Transactions;
  34. namespace System.ServiceModel.Channels
  35. {
  36. public class TransactionFlowBindingElement : BindingElement
  37. {
  38. TransactionProtocol protocol;
  39. // Funny, but since it uses OLE TX, Mono will never support this constructor.
  40. [MonoTODO]
  41. public TransactionFlowBindingElement ()
  42. : this (TransactionProtocol.Default)
  43. {
  44. }
  45. public TransactionFlowBindingElement (TransactionProtocol protocol)
  46. {
  47. this.protocol = protocol;
  48. }
  49. public TransactionProtocol TransactionProtocol {
  50. get { return protocol; }
  51. }
  52. public override BindingElement Clone ()
  53. {
  54. return new TransactionFlowBindingElement (protocol);
  55. }
  56. [MonoTODO]
  57. public override T GetProperty<T> (BindingContext context)
  58. {
  59. throw new NotImplementedException ();
  60. }
  61. public override bool CanBuildChannelFactory<TChannel> (BindingContext context)
  62. {
  63. return context.CanBuildInnerChannelFactory<TChannel> ();
  64. }
  65. [MonoTODO]
  66. public override bool CanBuildChannelListener<TChannel> (BindingContext context)
  67. {
  68. return context.CanBuildInnerChannelListener<TChannel> ();
  69. }
  70. public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (BindingContext context)
  71. {
  72. if (protocol == null)
  73. throw new InvalidOperationException ("Set transaction protocol in prior to build a channel factory.");
  74. if (protocol == TransactionProtocol.Default)
  75. throw new NotSupportedException ("Mono does not support DTC.");
  76. if (!CanBuildChannelFactory<TChannel> (context.Clone ()))
  77. throw new ArgumentException (String.Format ("The channel type '{0}' is not supported", typeof (TChannel)));
  78. return new TransactionChannelFactory<TChannel> (context.BuildInnerChannelFactory<TChannel> (), protocol);
  79. }
  80. public override IChannelListener<TChannel> BuildChannelListener<TChannel> (BindingContext context)
  81. {
  82. if (protocol == null)
  83. throw new InvalidOperationException ("Set transaction protocol in prior to build a channel listener.");
  84. if (protocol == TransactionProtocol.Default)
  85. throw new NotSupportedException ("Mono does not support DTC.");
  86. if (!CanBuildChannelListener<TChannel> (context.Clone ()))
  87. throw new ArgumentException (String.Format ("The channel type '{0}' is not supported", typeof (TChannel)));
  88. return new TransactionChannelListener<TChannel> (
  89. context.BuildInnerChannelListener<TChannel> (),
  90. protocol);
  91. }
  92. }
  93. internal class TransactionChannelFactory<TChannel> : ChannelFactoryBase<TChannel>
  94. {
  95. IChannelFactory<TChannel> inner_factory;
  96. TransactionScope txscope;
  97. TransactionProtocol protocol;
  98. public TransactionChannelFactory (IChannelFactory<TChannel> innerFactory, TransactionProtocol protocol)
  99. {
  100. this.inner_factory = innerFactory;
  101. this.protocol = protocol;
  102. }
  103. void ProcessOpen ()
  104. {
  105. CommittableTransaction tx = new CommittableTransaction ();
  106. txscope = new TransactionScope (tx);
  107. }
  108. protected override void OnOpen (TimeSpan timeout)
  109. {
  110. ProcessOpen ();
  111. inner_factory.Open (timeout);
  112. }
  113. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  114. {
  115. ProcessOpen ();
  116. return inner_factory.BeginOpen (timeout, callback, state);
  117. }
  118. protected override void OnEndOpen (IAsyncResult result)
  119. {
  120. inner_factory.EndOpen (result);
  121. }
  122. protected override TChannel OnCreateChannel (
  123. EndpointAddress remoteAddress, Uri via)
  124. {
  125. return inner_factory.CreateChannel (remoteAddress, via);
  126. }
  127. protected override void OnClose (TimeSpan timeout)
  128. {
  129. inner_factory.Close (timeout);
  130. txscope.Complete ();
  131. }
  132. }
  133. internal class TransactionChannelListener<TChannel> : ChannelListenerBase<TChannel> where TChannel : class, IChannel
  134. {
  135. IChannelListener<TChannel> inner_listener;
  136. TransactionScope txscope;
  137. TransactionProtocol protocol;
  138. public TransactionChannelListener (IChannelListener<TChannel> innerListener, TransactionProtocol protocol)
  139. {
  140. this.inner_listener = innerListener;
  141. this.protocol = protocol;
  142. }
  143. public override Uri Uri {
  144. get { return inner_listener.Uri; }
  145. }
  146. protected override void OnAbort ()
  147. {
  148. inner_listener.Abort ();
  149. }
  150. protected override void OnOpen (TimeSpan timeout)
  151. {
  152. CommittableTransaction tx = new CommittableTransaction ();
  153. txscope = new TransactionScope (tx);
  154. inner_listener.Open (timeout);
  155. }
  156. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  157. {
  158. return inner_listener.BeginOpen (timeout, callback, state);
  159. }
  160. protected override void OnEndOpen (IAsyncResult result)
  161. {
  162. inner_listener.EndOpen (result);
  163. }
  164. protected override void OnClose (TimeSpan timeout)
  165. {
  166. inner_listener.Close (timeout);
  167. txscope.Complete ();
  168. }
  169. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  170. {
  171. return inner_listener.BeginClose (timeout, callback, state);
  172. }
  173. protected override void OnEndClose (IAsyncResult result)
  174. {
  175. inner_listener.EndClose (result);
  176. }
  177. protected override bool OnWaitForChannel (TimeSpan timeout)
  178. {
  179. return inner_listener.WaitForChannel (timeout);
  180. }
  181. protected override IAsyncResult OnBeginWaitForChannel (TimeSpan timeout, AsyncCallback callback, object state)
  182. {
  183. return inner_listener.BeginWaitForChannel (timeout, callback, state);
  184. }
  185. protected override bool OnEndWaitForChannel (IAsyncResult result)
  186. {
  187. return inner_listener.EndWaitForChannel (result);
  188. }
  189. protected override TChannel OnAcceptChannel (TimeSpan timeout)
  190. {
  191. return inner_listener.AcceptChannel (timeout);
  192. }
  193. protected override IAsyncResult OnBeginAcceptChannel (TimeSpan timeout,
  194. AsyncCallback callback, object asyncState)
  195. {
  196. return inner_listener.BeginAcceptChannel (timeout, callback, asyncState);
  197. }
  198. protected override TChannel OnEndAcceptChannel (IAsyncResult result)
  199. {
  200. return inner_listener.EndAcceptChannel (result);
  201. }
  202. }
  203. }