Exemplo n.º 1
0
        public void ScopeContextException()
        {
            ScopeProvider scopeProvider = ScopeProvider;

            bool?completed = null;

            Assert.IsNull(scopeProvider.AmbientScope);
            using (IScope scope = scopeProvider.CreateScope())
            {
                IScope detached = scopeProvider.CreateDetachedScope();
                scopeProvider.AttachScope(detached);

                // the exception does not prevent other enlisted items to run
                // *and* it does not prevent the scope from properly going down
                scopeProvider.Context.Enlist("name", c => throw new Exception("bang"));
                scopeProvider.Context.Enlist("other", c => completed = c);
                detached.Complete();
                Assert.Throws <AggregateException>(() => detached.Dispose());

                // even though disposing of the scope has thrown, it has exited
                // properly ie it has removed itself, and the app remains clean
                Assert.AreSame(scope, scopeProvider.AmbientScope);
                scope.Complete();
            }

            Assert.IsNull(scopeProvider.AmbientScope);
            Assert.IsNull(scopeProvider.AmbientContext);

            Assert.IsNotNull(completed);
            Assert.AreEqual(true, completed);
        }
Exemplo n.º 2
0
        public void DetachableScope()
        {
            ScopeProvider scopeProvider = ScopeProvider;

            Assert.IsNull(scopeProvider.AmbientScope);
            using (IScope scope = scopeProvider.CreateScope())
            {
                Assert.IsInstanceOf <Scope>(scope);
                Assert.IsNotNull(scopeProvider.AmbientScope);
                Assert.AreSame(scope, scopeProvider.AmbientScope);

                Assert.IsNotNull(scopeProvider.AmbientContext); // the ambient context
                Assert.IsNotNull(scopeProvider.Context);        // the ambient context too (getter only)
                IScopeContext context = scopeProvider.Context;

                IScope detached = scopeProvider.CreateDetachedScope();
                scopeProvider.AttachScope(detached);

                Assert.AreEqual(detached, scopeProvider.AmbientScope);
                Assert.AreNotSame(context, scopeProvider.Context);

                // nesting under detached!
                using (IScope nested = scopeProvider.CreateScope())
                {
                    Assert.Throws <InvalidOperationException>(() =>

                                                              // cannot detach a non-detachable scope
                                                              scopeProvider.DetachScope());
                    nested.Complete();
                }

                Assert.AreEqual(detached, scopeProvider.AmbientScope);
                Assert.AreNotSame(context, scopeProvider.Context);

                // can detach
                Assert.AreSame(detached, scopeProvider.DetachScope());

                Assert.AreSame(scope, scopeProvider.AmbientScope);
                Assert.AreSame(context, scopeProvider.AmbientContext);

                Assert.Throws <InvalidOperationException>(() =>

                                                          // cannot disposed a non-attached scope
                                                          // in fact, only the ambient scope can be disposed
                                                          detached.Dispose());

                scopeProvider.AttachScope(detached);
                detached.Complete();
                detached.Dispose();

                // has self-detached, and is gone!
                Assert.AreSame(scope, scopeProvider.AmbientScope);
                Assert.AreSame(context, scopeProvider.AmbientContext);
            }

            Assert.IsNull(scopeProvider.AmbientScope);
            Assert.IsNull(scopeProvider.AmbientContext);
        }