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


C# Scope.WhenScopeEnds方法代码示例

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


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

示例1: 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

示例2: Dispose_RecursiveResolveTriggeredDuringEndScopeAction_StillDisposesRegisteredDisposables

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

            var scopedLifestyle = new FakeScopedLifestyle(scope);

            var container = new Container();

            var disposable = new DisposableObject();

            scope.RegisterForDisposal(disposable);

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

                return new DisposablePlugin();
            });

            container.GetInstance<IPlugin>();

            try
            {
                // Act
                scope.Dispose();

                Assert.Fail("Exception expected.");
            }
            catch
            {
                // Assert
                Assert.IsTrue(disposable.IsDisposedOnce,
                    "Even though there was a recursion detected when executing Action delegates, all " +
                    "registered disposables should still get disposed.");
            }
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:40,代码来源:ScopedLifestyleTests.cs

示例3: 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

示例4: 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

示例5: WhenScopeEnds_CalledOnDisposedScope_ThrowsObjectDisposedException

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

            scope.Dispose();

            // Act
            Action action = () => scope.WhenScopeEnds(() => { });

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

示例6: 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

示例7: 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

示例8: 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

示例9: Dispose_RegisteredActionAndRegisteredDisposableObject_CallsActionFirst

        public void Dispose_RegisteredActionAndRegisteredDisposableObject_CallsActionFirst()
        {
            // Arrange
            int actionCount = 0;
            int disposeCount = 0;

            var scope = new Scope();

            scope.RegisterForDisposal(new DisposableObject(_ =>
            {
                Assert.AreEqual(2, actionCount);
                disposeCount++;
            }));

            scope.WhenScopeEnds(() =>
            {
                Assert.AreEqual(0, disposeCount);
                actionCount++;
            });

            scope.WhenScopeEnds(() =>
            {
                Assert.AreEqual(0, disposeCount);
                actionCount++;
            });

            scope.RegisterForDisposal(new DisposableObject(_ =>
            {
                Assert.AreEqual(2, actionCount);
                disposeCount++;
            }));

            // Act
            scope.Dispose();

            // Assert
            Assert.AreEqual(2, actionCount);
            Assert.AreEqual(2, disposeCount);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:39,代码来源:ScopedLifestyleTests.cs


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