Ejemplo n.º 1
0
        public async Task Test_Async()
        {
            var scopeAccessor = new DataContextAmbientScopeProvider <TestData>(
                new AsyncLocalAmbientDataContext()
                );

            scopeAccessor.GetValue(ContextKey).ShouldBeNull();

            await Task.Delay(1);

            using (scopeAccessor.BeginScope(ContextKey, new TestData(42)))
            {
                await Task.Delay(1);

                scopeAccessor.GetValue(ContextKey).Number.ShouldBe(42);

                using (scopeAccessor.BeginScope(ContextKey, new TestData(24)))
                {
                    await Task.Delay(1);

                    scopeAccessor.GetValue(ContextKey).Number.ShouldBe(24);
                }

                await Task.Delay(1);

                scopeAccessor.GetValue(ContextKey).Number.ShouldBe(42);
            }

            await Task.Delay(1);

            scopeAccessor.GetValue(ContextKey).ShouldBeNull();
        }
        public void Test_Sync()
        {
            var scopeAccessor = new DataContextAmbientScopeProvider<TestData>(new CallContextAmbientDataContext());

            scopeAccessor.GetValue(ContextKey).ShouldBeNull();

            using (scopeAccessor.BeginScope(ContextKey, new TestData(42)))
            {
                scopeAccessor.GetValue(ContextKey).Number.ShouldBe(42);

                using (scopeAccessor.BeginScope(ContextKey, new TestData(24)))
                {
                    scopeAccessor.GetValue(ContextKey).Number.ShouldBe(24);
                }

                scopeAccessor.GetValue(ContextKey).Number.ShouldBe(42);
            }

            scopeAccessor.GetValue(ContextKey).ShouldBeNull();
        }