/
TransactionInteropNonWindows.cs
257 lines (205 loc) · 10.4 KB
/
TransactionInteropNonWindows.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.InteropServices;
using System.Transactions.Oletx;
namespace System.Transactions
{
public static class TransactionInterop
{
internal static OletxTransaction ConvertToOletxTransaction(Transaction transaction)
{
ArgumentNullException.ThrowIfNull(transaction);
ObjectDisposedException.ThrowIf(transaction.Disposed, transaction);
if (transaction._complete)
{
throw TransactionException.CreateTransactionCompletedException(transaction.DistributedTxId);
}
OletxTransaction? distributedTx = transaction.Promote();
if (distributedTx == null)
{
throw OletxTransaction.NotSupported();
}
return distributedTx;
}
/// <summary>
/// This is the PromoterType value that indicates that the transaction is promoting to MSDTC.
///
/// If using the variation of Transaction.EnlistPromotableSinglePhase that takes a PromoterType and the
/// ITransactionPromoter being used promotes to MSDTC, then this is the value that should be
/// specified for the PromoterType parameter to EnlistPromotableSinglePhase.
///
/// If using the variation of Transaction.EnlistPromotableSinglePhase that assumes promotion to MSDTC and
/// it that returns false, the caller can compare this value with Transaction.PromoterType to
/// verify that the transaction promoted, or will promote, to MSDTC. If the Transaction.PromoterType
/// matches this value, then the caller can continue with its enlistment with MSDTC. But if it
/// does not match, the caller will not be able to enlist with MSDTC.
/// </summary>
public static readonly Guid PromoterTypeDtc = new Guid("14229753-FFE1-428D-82B7-DF73045CB8DA");
public static byte[] GetExportCookie(Transaction transaction, byte[] whereabouts)
{
ArgumentNullException.ThrowIfNull(transaction);
ArgumentNullException.ThrowIfNull(whereabouts);
TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;
if (etwLog.IsEnabled())
{
etwLog.MethodEnter(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetExportCookie");
}
// Copy the whereabouts so that it cannot be modified later.
var whereaboutsCopy = new byte[whereabouts.Length];
Buffer.BlockCopy(whereabouts, 0, whereaboutsCopy, 0, whereabouts.Length);
ConvertToOletxTransaction(transaction);
byte[] cookie = OletxTransaction.GetExportCookie(whereaboutsCopy);
if (etwLog.IsEnabled())
{
etwLog.MethodExit(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetExportCookie");
}
return cookie;
}
public static Transaction GetTransactionFromExportCookie(byte[] cookie)
{
ArgumentNullException.ThrowIfNull(cookie);
if (cookie.Length < 32)
{
throw new ArgumentException(SR.InvalidArgument, nameof(cookie));
}
TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;
if (etwLog.IsEnabled())
{
etwLog.MethodEnter(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetTransactionFromExportCookie");
}
var cookieCopy = new byte[cookie.Length];
Buffer.BlockCopy(cookie, 0, cookieCopy, 0, cookie.Length);
cookie = cookieCopy;
// Extract the transaction guid from the propagation token to see if we already have a
// transaction object for the transaction.
// In a cookie, the transaction guid is preceded by a signature guid.
var txId = new Guid(cookie.AsSpan(16, 16));
// First check to see if there is a promoted LTM transaction with the same ID. If there
// is, just return that.
Transaction? transaction = TransactionManager.FindPromotedTransaction(txId);
if (transaction != null)
{
if (etwLog.IsEnabled())
{
etwLog.MethodExit(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetTransactionFromExportCookie");
}
return transaction;
}
// Find or create the promoted transaction.
OletxTransaction dTx = OletxTransactionManager.GetTransactionFromExportCookie(cookieCopy, txId);
transaction = TransactionManager.FindOrCreatePromotedTransaction(txId, dTx);
if (etwLog.IsEnabled())
{
etwLog.MethodExit(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetTransactionFromExportCookie");
}
return transaction;
}
public static byte[] GetTransmitterPropagationToken(Transaction transaction)
{
ArgumentNullException.ThrowIfNull(transaction);
TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;
if (etwLog.IsEnabled())
{
etwLog.MethodEnter(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetTransmitterPropagationToken");
}
ConvertToOletxTransaction(transaction);
byte[] token = OletxTransaction.GetTransmitterPropagationToken();
if (etwLog.IsEnabled())
{
etwLog.MethodExit(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetTransmitterPropagationToken");
}
return token;
}
public static Transaction GetTransactionFromTransmitterPropagationToken(byte[] propagationToken)
{
ArgumentNullException.ThrowIfNull(propagationToken);
if (propagationToken.Length < 24)
{
throw new ArgumentException(SR.InvalidArgument, nameof(propagationToken));
}
TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;
if (etwLog.IsEnabled())
{
etwLog.MethodEnter(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetTransactionFromTransmitterPropagationToken");
}
// Extract the transaction guid from the propagation token to see if we already have a
// transaction object for the transaction.
// In a propagation token, the transaction guid is preceded by two version DWORDs.
var txId = new Guid(propagationToken.AsSpan(8, 16));
// First check to see if there is a promoted LTM transaction with the same ID. If there is, just return that.
Transaction? tx = TransactionManager.FindPromotedTransaction(txId);
if (null != tx)
{
if (etwLog.IsEnabled())
{
etwLog.MethodExit(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetTransactionFromTransmitterPropagationToken");
}
return tx;
}
OletxTransaction dTx = GetOletxTransactionFromTransmitterPropagationToken(propagationToken);
// If a transaction is found then FindOrCreate will Dispose the distributed transaction created.
Transaction returnValue = TransactionManager.FindOrCreatePromotedTransaction(txId, dTx);
if (etwLog.IsEnabled())
{
etwLog.MethodExit(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetTransactionFromTransmitterPropagationToken");
}
return returnValue;
}
public static IDtcTransaction GetDtcTransaction(Transaction transaction)
{
ArgumentNullException.ThrowIfNull(transaction);
TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;
if (etwLog.IsEnabled())
{
etwLog.MethodEnter(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetDtcTransaction");
}
ConvertToOletxTransaction(transaction);
IDtcTransaction transactionNative = OletxTransaction.GetDtcTransaction();
if (etwLog.IsEnabled())
{
etwLog.MethodExit(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetDtcTransaction");
}
return transactionNative;
}
public static Transaction GetTransactionFromDtcTransaction(IDtcTransaction transactionNative)
{
ArgumentNullException.ThrowIfNull(transactionNative);
TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;
if (etwLog.IsEnabled())
{
etwLog.MethodEnter(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetTransactionFromDtcTransaction");
}
Transaction transaction = OletxTransactionManager.GetTransactionFromDtcTransaction(transactionNative);
if (etwLog.IsEnabled())
{
etwLog.MethodExit(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetTransactionFromDtcTransaction");
}
return transaction;
}
public static byte[] GetWhereabouts()
{
TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;
if (etwLog.IsEnabled())
{
etwLog.MethodEnter(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetWhereabouts");
}
byte[] returnValue = OletxTransactionManager.GetWhereabouts();
if (etwLog.IsEnabled())
{
etwLog.MethodExit(TraceSourceType.TraceSourceOleTx, "TransactionInterop.GetWhereabouts");
}
return returnValue;
}
internal static OletxTransaction GetOletxTransactionFromTransmitterPropagationToken(byte[] propagationToken)
{
ArgumentNullException.ThrowIfNull(propagationToken);
if (propagationToken.Length < 24)
{
throw new ArgumentException(SR.InvalidArgument, nameof(propagationToken));
}
byte[] propagationTokenCopy = new byte[propagationToken.Length];
Array.Copy(propagationToken, propagationTokenCopy, propagationToken.Length);
return OletxTransactionManager.GetOletxTransactionFromTransmitterPropagationToken(propagationTokenCopy);
}
}
}