TransactionProxy.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.ComIntegration
  5. {
  6. using System;
  7. using System.ServiceModel;
  8. using System.Transactions;
  9. using System.Diagnostics;
  10. using System.ServiceModel.Diagnostics;
  11. using System.Runtime.InteropServices;
  12. using SR = System.ServiceModel.SR;
  13. class TransactionProxyBuilder : IProxyCreator
  14. {
  15. ComProxy comProxy = null;
  16. TransactionProxy txProxy = null;
  17. private TransactionProxyBuilder(TransactionProxy proxy)
  18. {
  19. this.txProxy = proxy;
  20. }
  21. void IDisposable.Dispose()
  22. {
  23. }
  24. ComProxy IProxyCreator.CreateProxy(IntPtr outer, ref Guid riid)
  25. {
  26. if ((riid != typeof(ITransactionProxy).GUID))
  27. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidCastException(SR.GetString(SR.NoInterface, riid)));
  28. if (outer == IntPtr.Zero)
  29. {
  30. // transactions require failfasts to prevent corruption
  31. DiagnosticUtility.FailFast("OuterProxy cannot be null");
  32. }
  33. if (comProxy == null)
  34. {
  35. comProxy = ComProxy.Create(outer, txProxy, null);
  36. return comProxy;
  37. }
  38. else
  39. return comProxy.Clone();
  40. }
  41. bool IProxyCreator.SupportsErrorInfo(ref Guid riid)
  42. {
  43. if ((riid != typeof(ITransactionProxy).GUID))
  44. return false;
  45. else
  46. return true;
  47. }
  48. bool IProxyCreator.SupportsDispatch()
  49. {
  50. return false;
  51. }
  52. bool IProxyCreator.SupportsIntrinsics()
  53. {
  54. return false;
  55. }
  56. public static IntPtr CreateTransactionProxyTearOff(TransactionProxy txProxy)
  57. {
  58. IProxyCreator txProxyBuilder = new TransactionProxyBuilder(txProxy);
  59. IProxyManager proxyManager = new ProxyManager(txProxyBuilder);
  60. Guid iid = typeof(ITransactionProxy).GUID;
  61. return OuterProxyWrapper.CreateOuterProxyInstance(proxyManager, ref iid);
  62. }
  63. }
  64. class TransactionProxy : ITransactionProxy,
  65. IExtension<InstanceContext>
  66. {
  67. Transaction currentTransaction;
  68. VoterBallot currentVoter;
  69. object syncRoot;
  70. Guid appid;
  71. Guid clsid;
  72. int instanceID = 0;
  73. public TransactionProxy(Guid appid, Guid clsid)
  74. {
  75. this.syncRoot = new object();
  76. this.appid = appid;
  77. this.clsid = clsid;
  78. }
  79. public Transaction CurrentTransaction
  80. {
  81. get
  82. {
  83. return this.currentTransaction;
  84. }
  85. }
  86. public Guid AppId
  87. {
  88. get
  89. {
  90. return this.appid;
  91. }
  92. }
  93. public Guid Clsid
  94. {
  95. get
  96. {
  97. return this.clsid;
  98. }
  99. }
  100. public int InstanceID
  101. {
  102. get
  103. {
  104. return this.instanceID;
  105. }
  106. set
  107. {
  108. this.instanceID = value;
  109. }
  110. }
  111. public void SetTransaction(Transaction transaction)
  112. {
  113. lock (this.syncRoot)
  114. {
  115. if (transaction == null)
  116. {
  117. // transactions require failfasts to prevent corruption
  118. DiagnosticUtility.FailFast("Attempting to set transaction to NULL");
  119. }
  120. if (this.currentTransaction == null)
  121. {
  122. ProxyEnlistment enlistment;
  123. enlistment = new ProxyEnlistment(this, transaction);
  124. transaction.EnlistVolatile(enlistment, EnlistmentOptions.None);
  125. this.currentTransaction = transaction;
  126. if (this.currentVoter != null)
  127. {
  128. this.currentVoter.SetTransaction(this.currentTransaction);
  129. }
  130. }
  131. else if (this.currentTransaction != transaction)
  132. {
  133. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.TransactionMismatch());
  134. }
  135. }
  136. }
  137. // IExtension<ServiceInstance>
  138. public void Attach(InstanceContext owner) { }
  139. public void Detach(InstanceContext owner) { }
  140. // ITransactionProxy
  141. public void Commit(Guid guid)
  142. {
  143. // transactions require failfasts to prevent corruption
  144. DiagnosticUtility.FailFast("Commit not supported: BYOT only!");
  145. }
  146. public void Abort()
  147. {
  148. if (this.currentTransaction != null)
  149. {
  150. this.currentTransaction.Rollback();
  151. }
  152. }
  153. public IDtcTransaction Promote()
  154. {
  155. EnsureTransaction();
  156. return TransactionInterop.GetDtcTransaction(
  157. this.currentTransaction);
  158. }
  159. public void CreateVoter(
  160. ITransactionVoterNotifyAsync2 voterNotification,
  161. IntPtr voterBallot)
  162. {
  163. if (IntPtr.Zero == voterBallot)
  164. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("voterBallot");
  165. lock (this.syncRoot)
  166. {
  167. if (this.currentVoter != null)
  168. {
  169. // transactions require failfasts to prevent corruption
  170. DiagnosticUtility.FailFast("Assumption: proxy only needs one voter");
  171. }
  172. VoterBallot voter = new VoterBallot(voterNotification, this);
  173. if (this.currentTransaction != null)
  174. {
  175. voter.SetTransaction(this.currentTransaction);
  176. }
  177. this.currentVoter = voter;
  178. IntPtr ppv = InterfaceHelper.GetInterfacePtrForObject(typeof(ITransactionVoterBallotAsync2).GUID, this.currentVoter);
  179. Marshal.WriteIntPtr(voterBallot, ppv);
  180. }
  181. }
  182. public DtcIsolationLevel GetIsolationLevel()
  183. {
  184. DtcIsolationLevel retVal;
  185. switch (this.currentTransaction.IsolationLevel)
  186. {
  187. case IsolationLevel.Serializable:
  188. retVal = DtcIsolationLevel.ISOLATIONLEVEL_SERIALIZABLE;
  189. break;
  190. case IsolationLevel.RepeatableRead:
  191. retVal = DtcIsolationLevel.ISOLATIONLEVEL_REPEATABLEREAD;
  192. break;
  193. case IsolationLevel.ReadCommitted:
  194. retVal = DtcIsolationLevel.ISOLATIONLEVEL_READCOMMITTED;
  195. break;
  196. case IsolationLevel.ReadUncommitted:
  197. retVal = DtcIsolationLevel.ISOLATIONLEVEL_READUNCOMMITTED;
  198. break;
  199. default:
  200. retVal = DtcIsolationLevel.ISOLATIONLEVEL_SERIALIZABLE;
  201. break;
  202. }
  203. return retVal;
  204. }
  205. public Guid GetIdentifier()
  206. {
  207. return this.currentTransaction.TransactionInformation.DistributedIdentifier;
  208. }
  209. // ITransactionProxy2
  210. public bool IsReusable()
  211. {
  212. return true;
  213. }
  214. void ClearTransaction(ProxyEnlistment enlistment)
  215. {
  216. lock (this.syncRoot)
  217. {
  218. if (this.currentTransaction == null)
  219. {
  220. // transactions require failfasts to prevent corruption
  221. DiagnosticUtility.FailFast("Clearing inactive TransactionProxy");
  222. }
  223. if (enlistment.Transaction != this.currentTransaction)
  224. {
  225. // transactions require failfasts to prevent corruption
  226. DiagnosticUtility.FailFast("Incorrectly working on multiple transactions");
  227. }
  228. this.currentTransaction = null;
  229. this.currentVoter = null;
  230. }
  231. }
  232. void EnsureTransaction()
  233. {
  234. lock (this.syncRoot)
  235. {
  236. if (this.currentTransaction == null)
  237. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(null, HR.CONTEXT_E_NOTRANSACTION));
  238. }
  239. }
  240. class ProxyEnlistment : IEnlistmentNotification
  241. {
  242. TransactionProxy proxy;
  243. Transaction transaction;
  244. public ProxyEnlistment(TransactionProxy proxy,
  245. Transaction transaction)
  246. {
  247. this.proxy = proxy;
  248. this.transaction = transaction;
  249. }
  250. public Transaction Transaction
  251. {
  252. get { return this.transaction; }
  253. }
  254. public void Prepare(PreparingEnlistment preparingEnlistment)
  255. {
  256. this.proxy.ClearTransaction(this);
  257. this.proxy = null;
  258. preparingEnlistment.Done();
  259. }
  260. public void Commit(Enlistment enlistment)
  261. {
  262. // transactions require failfasts to prevent corruption
  263. DiagnosticUtility.FailFast("Should have voted read only");
  264. }
  265. public void Rollback(Enlistment enlistment)
  266. {
  267. this.proxy.ClearTransaction(this);
  268. this.proxy = null;
  269. enlistment.Done();
  270. }
  271. public void InDoubt(Enlistment enlistment)
  272. {
  273. // transactions require failfasts to prevent corruption
  274. DiagnosticUtility.FailFast("Should have voted read only");
  275. }
  276. }
  277. class VoterBallot : ITransactionVoterBallotAsync2, IEnlistmentNotification
  278. {
  279. const int S_OK = 0;
  280. ITransactionVoterNotifyAsync2 notification;
  281. Transaction transaction;
  282. Enlistment enlistment;
  283. PreparingEnlistment preparingEnlistment;
  284. TransactionProxy proxy;
  285. public VoterBallot(ITransactionVoterNotifyAsync2 notification, TransactionProxy proxy)
  286. {
  287. this.notification = notification;
  288. this.proxy = proxy;
  289. }
  290. public void SetTransaction(Transaction transaction)
  291. {
  292. if (this.transaction != null)
  293. {
  294. // transactions require failfasts to prevent corruption
  295. DiagnosticUtility.FailFast("Already have a transaction in the ballot!");
  296. }
  297. this.transaction = transaction;
  298. this.enlistment = transaction.EnlistVolatile(
  299. this,
  300. EnlistmentOptions.None);
  301. }
  302. public void Prepare(PreparingEnlistment enlistment)
  303. {
  304. this.preparingEnlistment = enlistment;
  305. this.notification.VoteRequest();
  306. }
  307. public void Rollback(Enlistment enlistment)
  308. {
  309. enlistment.Done();
  310. this.notification.Aborted(0, false, 0, S_OK);
  311. ComPlusTxProxyTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationTxProxyTxAbortedByTM,
  312. SR.TraceCodeComIntegrationTxProxyTxAbortedByTM, proxy.AppId, proxy.Clsid, transaction.TransactionInformation.DistributedIdentifier, proxy.InstanceID);
  313. Marshal.ReleaseComObject(this.notification);
  314. this.notification = null;
  315. }
  316. public void Commit(Enlistment enlistment)
  317. {
  318. enlistment.Done();
  319. this.notification.Committed(false, 0, S_OK);
  320. ComPlusTxProxyTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationTxProxyTxCommitted,
  321. SR.TraceCodeComIntegrationTxProxyTxCommitted, proxy.AppId, proxy.Clsid, transaction.TransactionInformation.DistributedIdentifier, proxy.InstanceID);
  322. Marshal.ReleaseComObject(this.notification);
  323. this.notification = null;
  324. }
  325. public void InDoubt(Enlistment enlistment)
  326. {
  327. enlistment.Done();
  328. this.notification.InDoubt();
  329. Marshal.ReleaseComObject(this.notification);
  330. this.notification = null;
  331. }
  332. public void VoteRequestDone(int hr, int reason)
  333. {
  334. if (this.preparingEnlistment == null)
  335. {
  336. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  337. SR.GetString(SR.NoVoteIssued)));
  338. }
  339. if (S_OK == hr)
  340. {
  341. this.preparingEnlistment.Prepared();
  342. }
  343. else
  344. {
  345. this.preparingEnlistment.ForceRollback();
  346. ComPlusTxProxyTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationTxProxyTxAbortedByContext,
  347. SR.TraceCodeComIntegrationTxProxyTxAbortedByContext, proxy.AppId, proxy.Clsid, transaction.TransactionInformation.DistributedIdentifier, proxy.InstanceID);
  348. }
  349. }
  350. }
  351. }
  352. }