コード例 #1
0
        public void InstancesActivatedWithinScopeAreDeactivatedAfterThreadIsGarbageCollectedAndCacheIsPruned()
        {
            this.kernel.Bind <NotifiesWhenDisposed>().ToSelf().InThreadScope();
            var cache = this.kernel.Components.Get <ICache>();

            NotifiesWhenDisposed instance = null;

            ThreadStart callback = () => instance = this.kernel.Get <NotifiesWhenDisposed>();

            var thread = new Thread(callback);

            thread.Start();
            thread.Join();

            thread = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            cache.Prune();

            instance.Should().NotBeNull();
            instance.IsDisposed.Should().BeTrue();
        }
コード例 #2
0
        public void InstanceIsNotTrackedForDeactivation()
        {
            var instance = new NotifiesWhenDisposed();

            kernel.Inject(instance);
            kernel.Dispose();

            instance.IsDisposed.Should().BeFalse();
        }
コード例 #3
0
        public void StrategyDisposesInstanceIfItIsDisposable()
        {
            var instance  = new NotifiesWhenDisposed();
            var reference = new InstanceReference {
                Instance = instance
            };

            this.strategy.Deactivate(this.contextMock.Object, reference);
            instance.IsDisposed.Should().BeTrue();
        }
コード例 #4
0
ファイル: ThreadScopeTests.cs プロジェクト: skalinets/ninject
        private NotifiesWhenDisposed GetInstanceFromSeparateThread()
        {
            NotifiesWhenDisposed instance = null;

            ThreadStart callback = () => instance = this.kernel.Get <NotifiesWhenDisposed>();

            var thread = new Thread(callback);

            thread.Start();
            thread.Join();

            return(instance);
        }
コード例 #5
0
ファイル: ThreadScopeTests.cs プロジェクト: skalinets/ninject
        public void InstancesActivatedWithinScopeAreDeactivatedAfterThreadIsGarbageCollectedAndCacheIsPruned()
        {
            this.kernel.Bind <NotifiesWhenDisposed>().ToSelf().InThreadScope();
            var cache = this.kernel.Components.Get <ICache>();

            // Use separate method to allow thread/scope to be finalized
            NotifiesWhenDisposed instance = GetInstanceFromSeparateThread();

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            cache.Prune();

            instance.Should().NotBeNull();
            instance.IsDisposed.Should().BeTrue();
        }
コード例 #6
0
        public void DisposalCascadesThroughScopesThatNotifyWhenDisposed()
        {
            MobileInfantry     rico;
            MoritaAssaultRifle rifle;
            var scope  = new NotifiesWhenDisposed();
            var kernel = new StandardKernel(new NinjectSettings()
            {
                LoadExtensions = false
            });

            kernel.Bind <IWarrior>().To <MobileInfantry>().InScope(ctx => RequestScope.Current).WithParameter(new Parameter("scope", scope, true)).OnDeactivation(_ => scope.Dispose());
            kernel.Bind <IWeapon>().To <MoritaAssaultRifle>().InScope(ctx => ((Parameter)ctx.Parameters.First()).ValueCallback(ctx, null));

            using (new RequestScope())
            {
                rico  = (MobileInfantry)kernel.Get <IWarrior>();
                rifle = (MoritaAssaultRifle)rico.Weapon;
            }

            // if scopes are chained to the request scope "properly", then all services
            // are disposed when the request scope is disposed
            rifle.IsDisposed.Should().BeTrue();
        }