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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.Activities
{
using System;
using System.Activities.Runtime;
using System.Diagnostics.CodeAnalysis;
using System.Runtime;
using System.Runtime.Serialization;
using System.Transactions;
[Fx.Tag.XamlVisible(false)]
[DataContract]
public sealed class RuntimeTransactionHandle : Handle, IExecutionProperty, IPropertyRegistrationCallback
{
ActivityExecutor executor;
bool isHandleInitialized;
bool doNotAbort;
bool isPropertyRegistered;
bool isSuppressed;
TransactionScope scope;
Transaction rootTransaction;
public RuntimeTransactionHandle()
{
}
// This ctor is used when we want to make a transaction ambient
// without enlisting. This is desirable for scenarios like WorkflowInvoker
public RuntimeTransactionHandle(Transaction rootTransaction)
{
if (rootTransaction == null)
{
throw FxTrace.Exception.ArgumentNull("rootTransaction");
}
this.rootTransaction = rootTransaction;
this.AbortInstanceOnTransactionFailure = false;
}
public bool AbortInstanceOnTransactionFailure
{
get
{
return !this.doNotAbort;
}
set
{
ThrowIfRegistered(SR.CannotChangeAbortInstanceFlagAfterPropertyRegistration);
this.doNotAbort = !value;
}
}
public bool SuppressTransaction
{
get
{
return this.isSuppressed;
}
set
{
ThrowIfRegistered(SR.CannotSuppressAlreadyRegisteredHandle);
this.isSuppressed = value;
}
}
[DataMember(Name = "executor")]
internal ActivityExecutor SerializedExecutor
{
get { return this.executor; }
set { this.executor = value; }
}
[DataMember(EmitDefaultValue = false, Name = "isHandleInitialized")]
internal bool SerializedIsHandleInitialized
{
get { return this.isHandleInitialized; }
set { this.isHandleInitialized = value; }
}
[DataMember(EmitDefaultValue = false, Name = "doNotAbort")]
internal bool SerializedDoNotAbort
{
get { return this.doNotAbort; }
set { this.doNotAbort = value; }
}
[DataMember(EmitDefaultValue = false, Name = "isPropertyRegistered")]
internal bool SerializedIsPropertyRegistered
{
get { return this.isPropertyRegistered; }
set { this.isPropertyRegistered = value; }
}
[DataMember(EmitDefaultValue = false, Name = "isSuppressed")]
internal bool SerializedIsSuppressed
{
get { return this.isSuppressed; }
set { this.isSuppressed = value; }
}
internal bool IsRuntimeOwnedTransaction
{
get { return this.rootTransaction != null; }
}
void ThrowIfRegistered(string message)
{
if (this.isPropertyRegistered)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(message));
}
}
void ThrowIfNotRegistered(string message)
{
if (!this.isPropertyRegistered)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(message));
}
}
[SuppressMessage(FxCop.Category.Design, FxCop.Rule.ConsiderPassingBaseTypesAsParameters,
Justification = "This method is designed to be called from activities with handle access.")]
public Transaction GetCurrentTransaction(NativeActivityContext context)
{
return GetCurrentTransactionCore(context);
}
[SuppressMessage(FxCop.Category.Design, FxCop.Rule.ConsiderPassingBaseTypesAsParameters,
Justification = "This method is designed to be called from activities with handle access.")]
public Transaction GetCurrentTransaction(CodeActivityContext context)
{
return GetCurrentTransactionCore(context);
}
[SuppressMessage(FxCop.Category.Design, FxCop.Rule.ConsiderPassingBaseTypesAsParameters,
Justification = "This method is designed to be called from activities with handle access.")]
public Transaction GetCurrentTransaction(AsyncCodeActivityContext context)
{
return GetCurrentTransactionCore(context);
}
Transaction GetCurrentTransactionCore(ActivityContext context)
{
if (context == null)
{
throw FxTrace.Exception.ArgumentNull("context");
}
context.ThrowIfDisposed();
//If the transaction is a runtime transaction (i.e. an Invoke with ambient transaction case), then
//we do not require that it be registered since the handle created for the root transaction is never registered.
if (this.rootTransaction == null)
{
this.ThrowIfNotRegistered(SR.RuntimeTransactionHandleNotRegisteredAsExecutionProperty("GetCurrentTransaction"));
}
if (!this.isHandleInitialized)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.UnInitializedRuntimeTransactionHandle));
}
if (this.SuppressTransaction)
{
return null;
}
return this.executor.CurrentTransaction;
}
protected override void OnInitialize(HandleInitializationContext context)
{
this.executor = context.Executor;
this.isHandleInitialized = true;
if (this.rootTransaction != null)
{
Fx.Assert(this.Owner == null, "this.rootTransaction should only be set at the root");
this.executor.SetTransaction(this, this.rootTransaction, null, null);
}
base.OnInitialize(context);
}
protected override void OnUninitialize(HandleInitializationContext context)
{
if (this.rootTransaction != null)
{
// If we have a host transaction we're responsible for exiting no persist
this.executor.ExitNoPersist();
}
this.isHandleInitialized = false;
base.OnUninitialize(context);
}
public void RequestTransactionContext(NativeActivityContext context, Action<NativeActivityTransactionContext, object> callback, object state)
{
RequestOrRequireTransactionContextCore(context, callback, state, false);
}
public void RequireTransactionContext(NativeActivityContext context, Action<NativeActivityTransactionContext, object> callback, object state)
{
RequestOrRequireTransactionContextCore(context, callback, state, true);
}
void RequestOrRequireTransactionContextCore(NativeActivityContext context, Action<NativeActivityTransactionContext, object> callback, object state, bool isRequires)
{
if (context == null)
{
throw FxTrace.Exception.ArgumentNull("context");
}
context.ThrowIfDisposed();
if (context.HasRuntimeTransaction)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.RuntimeTransactionAlreadyExists));
}
if (context.IsInNoPersistScope)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.CannotSetRuntimeTransactionInNoPersist));
}
if (!this.isHandleInitialized)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.UnInitializedRuntimeTransactionHandle));
}
if (this.SuppressTransaction)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.RuntimeTransactionIsSuppressed));
}
if (isRequires)
{
if (context.RequiresTransactionContextWaiterExists)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.OnlyOneRequireTransactionContextAllowed));
}
this.ThrowIfNotRegistered(SR.RuntimeTransactionHandleNotRegisteredAsExecutionProperty("RequireTransactionContext"));
}
else
{
this.ThrowIfNotRegistered(SR.RuntimeTransactionHandleNotRegisteredAsExecutionProperty("RequestTransactionContext"));
}
context.RequestTransactionContext(isRequires, this, callback, state);
}
public void CompleteTransaction(NativeActivityContext context)
{
CompleteTransactionCore(context, null);
}
public void CompleteTransaction(NativeActivityContext context, BookmarkCallback callback)
{
if (callback == null)
{
throw FxTrace.Exception.ArgumentNull("callback");
}
CompleteTransactionCore(context, callback);
}
void CompleteTransactionCore(NativeActivityContext context, BookmarkCallback callback)
{
context.ThrowIfDisposed();
if (this.rootTransaction != null)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.CannotCompleteRuntimeOwnedTransaction));
}
if (!context.HasRuntimeTransaction)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.NoRuntimeTransactionExists));
}
if (!this.isHandleInitialized)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.UnInitializedRuntimeTransactionHandle));
}
if (this.SuppressTransaction)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.RuntimeTransactionIsSuppressed));
}
context.CompleteTransaction(this, callback);
}
[Fx.Tag.Throws(typeof(TransactionException), "The transaction for this property is in a state incompatible with TransactionScope.")]
void IExecutionProperty.SetupWorkflowThread()
{
if (this.SuppressTransaction)
{
this.scope = new TransactionScope(TransactionScopeOption.Suppress);
return;
}
if ((this.executor != null) && this.executor.HasRuntimeTransaction)
{
this.scope = TransactionHelper.CreateTransactionScope(this.executor.CurrentTransaction);
}
}
void IExecutionProperty.CleanupWorkflowThread()
{
TransactionHelper.CompleteTransactionScope(ref this.scope);
}
void IPropertyRegistrationCallback.Register(RegistrationContext context)
{
if (!this.isHandleInitialized)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.UnInitializedRuntimeTransactionHandle));
}
RuntimeTransactionHandle handle = (RuntimeTransactionHandle)context.FindProperty(typeof(RuntimeTransactionHandle).FullName);
if (handle != null)
{
if (handle.SuppressTransaction)
{
this.isSuppressed = true;
}
}
this.isPropertyRegistered = true;
}
void IPropertyRegistrationCallback.Unregister(RegistrationContext context)
{
this.isPropertyRegistered = false;
}
}
}
|