当前位置: 首页>>代码示例>>C#>>正文


C# Scope.Dispose方法代码示例

本文整理汇总了C#中Scope.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# Scope.Dispose方法的具体用法?C# Scope.Dispose怎么用?C# Scope.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Scope的用法示例。


在下文中一共展示了Scope.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Dispose_ListWithOneItem_DisposesItem

        public void Dispose_ListWithOneItem_DisposesItem()
        {
            // Arrange
            var scope = new Scope();

            var disposable = new DisposableObject();

            Assert.IsFalse(disposable.IsDisposedOnce, "Test setup");

            var disposables = new List<IDisposable> { disposable };

            scope.RegisterForDisposal(disposable);

            // Act
            scope.Dispose();

            // Assert
            Assert.IsTrue(disposable.IsDisposedOnce);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:19,代码来源:ScopedLifestyleTests.cs

示例2: Dispose_MultipleItems_DisposesAllItems

        public void Dispose_MultipleItems_DisposesAllItems()
        {
            // Arrange
            var scope = new Scope();

            var disposables = new List<DisposableObject> 
            { 
                new DisposableObject(),
                new DisposableObject(),
                new DisposableObject()
            };

            disposables.ForEach(scope.RegisterForDisposal);

            // Act
            scope.Dispose();

            // Assert
            Assert.IsTrue(disposables.All(d => d.IsDisposedOnce));
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:20,代码来源:ScopedLifestyleTests.cs

示例3: ApplyResolveScope

        private static object ApplyResolveScope(InitializationContext context, Func<object> getInstance)
        {
            var threadLocal = (ThreadLocal<Scope>)context.Registration.Container.GetItem(Key);

            var original = threadLocal.Value;
            var current = new Scope();

            try
            {
                threadLocal.Value = current;
                return getInstance();
            }
            finally
            {
                try
                {
                    current.Dispose();
                }
                finally
                {
                    threadLocal.Value = original;
                }
            }
        }
开发者ID:khellang,项目名称:SimpleInjector,代码行数:24,代码来源:PerResolveLifestyle.cs

示例4: Dispose_RecursiveResolveTriggeredInDispose_ThrowsDescriptiveException

        public void Dispose_RecursiveResolveTriggeredInDispose_ThrowsDescriptiveException()
        {
            // Arrange
            var scope = new Scope();

            var scopedLifestyle = new FakeScopedLifestyle(scope);

            var container = new Container();

            container.Register<IPlugin>(() =>
            {
                var plugin = new DisposablePlugin(disposing: _ =>
                {
                    // Recursive dependency
                    // Although really bad practice, this must not cause an infinit spin or a stackoverflow.
                    container.GetInstance<IPlugin>();
                });

                scope.RegisterForDisposal(plugin);

                return plugin;
            });

            container.GetInstance<IPlugin>();

            // Act
            Action action = () => scope.Dispose();

            // Assert
            AssertThat.ThrowsWithExceptionMessageContains<ActivationException>(@"
                The registered delegate for type IPlugin threw an exception. A recursive registration of 
                Action or IDisposable instances was detected during disposal of the scope. 
                This is possibly caused by a component that is directly or indirectly depending on itself"
                .TrimInside(),
                action);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:36,代码来源:ScopedLifestyleTests.cs

示例5: Dispose_ExceptionThrownDuringDisposalAfterResolvingNewInstance_DisposesThatInstance

        public void Dispose_ExceptionThrownDuringDisposalAfterResolvingNewInstance_DisposesThatInstance()
        {
            // Arrange
            bool newlyResolvedInstanceDisposed = false;

            var scope = new Scope();

            var scopedLifestyle = new FakeScopedLifestyle(scope);

            var container = new Container();

            container.Register<IPlugin>(
                () => new DisposablePlugin(disposing: _ => newlyResolvedInstanceDisposed = true),
                scopedLifestyle);

            container.Register<DisposableObject>(() => new DisposableObject(disposing: _ =>
                {
                    container.GetInstance<IPlugin>();
                    throw new Exception("Bang!");
                }), scopedLifestyle);

            container.GetInstance<DisposableObject>();

            try
            {
                // Act
                scope.Dispose();

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch
            {
                Assert.IsTrue(newlyResolvedInstanceDisposed);
            }
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:36,代码来源:ScopedLifestyleTests.cs

示例6: Dispose_ExceptionThrownDuringDisposalAfterResolvingNewInstance_DoesNotCallAnyNewActions

        public void Dispose_ExceptionThrownDuringDisposalAfterResolvingNewInstance_DoesNotCallAnyNewActions()
        {
            // Arrange
            bool scopeEndActionCalled = false;

            var scope = new Scope();

            var scopedLifestyle = new FakeScopedLifestyle(scope);

            var container = new Container();

            container.Register<IPlugin>(() => new DisposablePlugin());
            container.RegisterInitializer<IPlugin>(
                plugin => scope.WhenScopeEnds(() => scopeEndActionCalled = true));

            container.Register<DisposableObject>(() => new DisposableObject(_ =>
            {
                container.GetInstance<IPlugin>();
                throw new Exception("Bang!");
            }), scopedLifestyle);

            try
            {
                // Act
                scope.Dispose();

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch
            {
                Assert.IsFalse(scopeEndActionCalled,
                    "In case of an exception, no actions will be further executed. This lowers the change " +
                    "the new exceptions are thrown from other actions that cover up the original exception.");
            }
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:36,代码来源:ScopedLifestyleTests.cs

示例7: ShouldThrowExceptionWhenEndingNonCurrentScope

        public void ShouldThrowExceptionWhenEndingNonCurrentScope()
        {
            var container = CreateContainer();

            using (container.BeginScope())
            {
                var scope = new Scope(container.ScopeManagerProvider.GetScopeManager(container), null);
                Assert.Throws<InvalidOperationException>(() => scope.Dispose());
            }
        }
开发者ID:rubenwe,项目名称:LightInject,代码行数:10,代码来源:ScopeGetInstanceTests.cs

示例8: Dispose_InstanceResolvedDuringDisposingScopeRegisteringEndAction_CallsThisAction

        public void Dispose_InstanceResolvedDuringDisposingScopeRegisteringEndAction_CallsThisAction()
        {
            // Arrange
            bool actionCalled = false;

            var scope = new Scope();

            var scopedLifestyle = new FakeScopedLifestyle(scope);

            var container = new Container();

            container.Register<DisposableObject>(() => new DisposableObject(_ =>
            {
                container.GetInstance<IPlugin>();
            }), scopedLifestyle);

            container.Register<IPlugin>(() =>
            {
                scope.WhenScopeEnds(() => actionCalled = true);
                return new DisposablePlugin();
            }, Lifestyle.Transient);

            container.GetInstance<DisposableObject>();

            // Act
            scope.Dispose();

            // Assert
            Assert.IsTrue(actionCalled);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:30,代码来源:ScopedLifestyleTests.cs

示例9: GetInstance_CalledOnADisposedScope_ThrowsObjectDisposedException

        public void GetInstance_CalledOnADisposedScope_ThrowsObjectDisposedException()
        {
            // Arrange
            var scope = new Scope();

            var scopedLifestyle = new FakeScopedLifestyle(scope);

            var container = new Container();

            container.Register<IPlugin>(() => new DisposablePlugin(), scopedLifestyle);

            container.GetInstance<IPlugin>();

            scope.Dispose();

            // Act
            Action action = () => container.GetInstance<IPlugin>();

            // Assert
            AssertThat.ThrowsWithExceptionMessageContains<ActivationException>(
                CannotAccessADisposedObjectMessage,
                action);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:23,代码来源:ScopedLifestyleTests.cs

示例10: Container

        public void GetInstance_ResolvingScopedInstanceWhileDifferentThreadIsVerifying_DoesNotResolveInstanceFromVerificationScope()
        {
            // Arrange
            DisposablePlugin verifiedPlugin = null;
            DisposablePlugin backgroundResolvedPlugin = null;
            Task task = null;

            var container = new Container();

            var scope = new Scope();
            var lifestyle = new FakeScopedLifestyle(scope);

            container.Register<IPlugin, DisposablePlugin>(lifestyle);
            container.RegisterInitializer<DisposablePlugin>(p =>
            {
                verifiedPlugin = p;

                // Resolve on a different thread (so not during verification)
                task = Task.Run(() =>
                {
                    backgroundResolvedPlugin = (DisposablePlugin)container.GetInstance<IPlugin>();
                });

                Thread.Sleep(150);
            });

            // Act
            container.Verify();

            task.Wait();

            // Assert
            Assert.IsFalse(backgroundResolvedPlugin.IsDisposedOnce,
                "Since this instance isn't resolved during verification, but within an active scope, " +
                "The instance should not have been disposed here.");

            scope.Dispose();

            Assert.IsTrue(backgroundResolvedPlugin.IsDisposedOnce, "Now it should have been disposed.");
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:40,代码来源:ScopedLifestyleTests.cs

示例11: RegisterForDisposal_CalledOnDisposedScope_ThrowsObjectDisposedException

        public void RegisterForDisposal_CalledOnDisposedScope_ThrowsObjectDisposedException()
        {
            // Arrange
            var scope = new Scope();

            scope.Dispose();

            // Act
            Action action = () => scope.RegisterForDisposal(new DisposableObject());

            // Assert
            AssertThat.Throws<ObjectDisposedException>(action,
                "Calling RegisterForDisposal should throw an ObjectDisposedException.");
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:14,代码来源:ScopedLifestyleTests.cs

示例12: Dispose_RecursiveResolveTriggeredDuringEndScopeAction_ThrowsDescriptiveException

        public void Dispose_RecursiveResolveTriggeredDuringEndScopeAction_ThrowsDescriptiveException()
        {
            // Arrange
            var scope = new Scope();

            var scopedLifestyle = new FakeScopedLifestyle(scope);

            var container = new Container();

            container.Register<IPlugin>(() =>
            {
                scope.WhenScopeEnds(() =>
                {
                    container.GetInstance<IPlugin>();
                });

                return new DisposablePlugin();
            });

            container.GetInstance<IPlugin>();

            // Act
            Action action = () => scope.Dispose();

            // Assert
            AssertThat.ThrowsWithExceptionMessageContains<ActivationException>(@"
                The registered delegate for type IPlugin threw an exception. A recursive registration of 
                Action or IDisposable instances was detected during disposal of the scope. 
                This is possibly caused by a component that is directly or indirectly depending on itself"
                .TrimInside(),
                action);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:32,代码来源:ScopedLifestyleTests.cs

示例13: Dispose_WhenDisposeEndsActionRegisteredDuringDisposal_CallsTheRegisteredDelegate

        public void Dispose_WhenDisposeEndsActionRegisteredDuringDisposal_CallsTheRegisteredDelegate()
        {
            // Arrange
            bool actionCalled = false;

            var disposable = new DisposableObject();

            var scope = new Scope();

            var container = new Container();

            container.Register<DisposableObject>(() => disposable, new FakeScopedLifestyle(scope));
            container.RegisterInitializer<DisposableObject>(instance =>
            {
                scope.WhenScopeEnds(() => actionCalled = true);
            });

            scope.WhenScopeEnds(() => container.GetInstance<DisposableObject>());

            // Act
            scope.Dispose();

            // Assert
            Assert.IsTrue(actionCalled);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:25,代码来源:ScopedLifestyleTests.cs

示例14: Dispose_ScopedItemResolvedDuringScopeEndAction_GetsDisposed

        public void Dispose_ScopedItemResolvedDuringScopeEndAction_GetsDisposed()
        {
            // Arrange
            var disposable = new DisposableObject();

            var scope = new Scope();

            var container = new Container();

            container.Register<DisposableObject>(() => disposable, new FakeScopedLifestyle(scope));

            scope.WhenScopeEnds(() => container.GetInstance<DisposableObject>());

            // Act
            scope.Dispose();

            // Assert
            Assert.IsTrue(disposable.IsDisposedOnce);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:19,代码来源:ScopedLifestyleTests.cs

示例15: Dispose_WithThrowingAction_StillDisposesInstance

        public void Dispose_WithThrowingAction_StillDisposesInstance()
        {
            // Arrange
            bool disposed = false;

            var scope = new Scope();

            scope.WhenScopeEnds(() =>
            {
                throw new Exception();
            });

            scope.RegisterForDisposal(new DisposableObject(_ =>
            {
                disposed = true;
            }));

            try
            {
                // Act
                scope.Dispose();

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch
            {
                Assert.IsTrue(disposed);
            }
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:30,代码来源:ScopedLifestyleTests.cs


注:本文中的Scope.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。