protected ScopeContext(string contextName, ScopeContextPolicy policy) { _policy = policy; _contextName = contextName; var contextObject = CallContext.LogicalGetData(contextName) as T; if (contextObject != null) { // Nested if (_policy == ScopeContextPolicy.MustBeRoot) { throw new SoftwareException("A {0} was already declared within the calling context", typeof(T).Name); } IsRootScope = false; RootScope = contextObject; } else { // Root if (_policy == ScopeContextPolicy.MustBeNested) { throw new SoftwareException("No {0} was declared in the calling context", typeof(T).Name); } IsRootScope = true; RootScope = (T)this; CallContext.LogicalSetData(contextName, this); } }
private bool ExceptionOccuredAsync(ScopeContextPolicy rootPolicy, ScopeContextPolicy childPolicy, int delay1 = 0, int delay2 = 0, int delay3 = 0) { try { AsyncTest(Tuple.Create(rootPolicy, delay1), Tuple.Create(childPolicy, delay2), Tuple.Create(ScopeContextPolicy.None, delay3)).Wait(); } catch (Exception error) { return(true); } return(false); }
public ScopeContextTest(ScopeContextPolicy policy) : base("myTlsSlotName", policy) { if (IsRootScope) { TextWriter.WriteLine("[Begin Root Scope]"); } else { TextWriter.WriteLine("[Begin Child Scope]"); } }
public static DACScope BeginScope(this IDAC dac, bool openConnection = true, ScopeContextPolicy policy = ScopeContextPolicy.None) { if (dac.UseScopeOsmosis) { return(new DACScope(dac, policy, openConnection)); } if (policy == ScopeContextPolicy.MustBeNested) { throw new ArgumentException("Policy cannot be MustBeNested for DAC that uses direct connections (i.e. UseScopeOsmosis == false).", "policy"); } return(new DACScope(dac, ScopeContextPolicy.None, openConnection, string.Format("{0}:", dac.InstanceID.ToStrictAlphaString()))); }
internal DACScope(IDAC dac, ScopeContextPolicy policy, bool openConnection, string contextPrefix = DefaultContextPrefix, Auto?autoAction = null) : base(string.Format(ContextNameTemplate, contextPrefix, dac.ConnectionString), policy) { if (dac == null) { throw new ArgumentNullException("dac"); } DAC = dac; if (IsRootScope) { _connection = new RestrictedConnection(DAC.CreateConnection()); if (openConnection) { _connection.Open(); } _scopeOwnsConnection = true; _transaction = null; _scopeOwnsTransaction = false; _transactionOwner = null; } else { if (RootScope._connection == null) { throw new SoftwareException("Internal Error: RootScope DAC had null connection"); } _connection = RootScope._connection; _transaction = RootScope._transaction; _transactionOwner = RootScope._transactionOwner; _scopeOwnsTransaction = false; _scopeOwnsConnection = false; if (openConnection && _connection.State.IsIn(ConnectionState.Closed, ConnectionState.Broken)) { _connection.Open(); } } _withinSystemTransactionScope = System.Transactions.Transaction.Current != null; if (_scopeOwnsConnection && _withinSystemTransactionScope) { DAC.EnlistInSystemTransaction(_connection.DangerousInternalConnection, System.Transactions.Transaction.Current); } _voteRollback = false; _scopeHasOpenTransaction = false; _autoAction = autoAction; }
private bool ExceptionOccured(ScopeContextPolicy rootPolicy, ScopeContextPolicy childPolicy, int delay1 = 0, int delay2 = 0, int delay3 = 0) { try { using (new ScopeContextDemo(rootPolicy)) { System.Threading.Thread.Sleep(delay1); using (new ScopeContextDemo(childPolicy)) { System.Threading.Thread.Sleep(delay2); using (new ScopeContextDemo(childPolicy)) { System.Threading.Thread.Sleep(delay3); } } } } catch (Exception error) { return(true); } return(false); }
private static void TestScopePolicy(TextBoxWriter writer, ScopeContextPolicy rootPolicy, ScopeContextPolicy childPolicy) { try { ScopeContextTest.TextWriter = writer; writer.WriteLine("Testing Scoped Context: RootPolicy={0}, ChildPolicy={1}", rootPolicy, childPolicy); using (new ScopeContextTest(rootPolicy)) { using (new ScopeContextTest(childPolicy)) { using (new ScopeContextTest(childPolicy)) { } } } } catch (Exception error) { writer.WriteLine("Exception: {0}", error.ToDisplayString()); } finally { writer.WriteLine(); writer.WriteLine(); } }
public ScopeContextDemo(ScopeContextPolicy policy) : base("ScopedContextDemo", policy) { }