Exemplo n.º 1
0
        public void DisposalScope_ThrowExceptionDuringCleanUp()
        {
            var disposalScope     = new DisposalScope();
            var disposableService = new DisposableService();
            var eventFired        = false;

            disposableService.Disposing += (sender, args) => eventFired = true;

            disposalScope.AddDisposable(disposableService, disposed => { throw new Exception(); });

            Assert.ThrowsAny <AggregateException>(() => disposalScope.Dispose());
        }
Exemplo n.º 2
0
        public void BasicDisposalTest()
        {
            DisposalScope disposalScope = new DisposalScope();
            DisposableService disposableService = new DisposableService();
            bool eventFired = false;

            disposableService.Disposing += (sender, args) => eventFired = true;

            disposalScope.AddDisposable(disposableService);

            disposalScope.Dispose();

            Assert.True(eventFired);
        }
Exemplo n.º 3
0
        public void DisposalScope_DisposeCalledTest()
        {
            var disposalScope     = new DisposalScope();
            var disposableService = new DisposableService();
            var eventFired        = false;

            disposableService.Disposing += (sender, args) => eventFired = true;

            disposalScope.AddDisposable(disposableService);

            disposalScope.Dispose();

            Assert.True(eventFired);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Get disposal scope from http context
        /// </summary>
        /// <param name="locatorScope"></param>
        /// <returns></returns>
        public static IDisposalScope GetDisposalScopeFromHttpContext(IExportLocatorScope locatorScope)
        {
            if (HttpContext.Current == null)
            {
                return(locatorScope);
            }

            var disposalScope = HttpContext.Current.Items[_disposalScopeKey] as IDisposalScope;

            if (disposalScope != null)
            {
                return(disposalScope);
            }

            disposalScope = new DisposalScope();

            HttpContext.Current.Items[_disposalScopeKey] = disposalScope;

            return(disposalScope);
        }
Exemplo n.º 5
0
        public void DisposableScope_WithNullCleanup_Threading_Test()
        {
            DisposableItem.CreateCount  = 0;
            DisposableItem.DisposeCount = 0;

            var scope = new DisposalScope();

            var syncEvent = new ManualResetEvent(false);

            var task1 = Task.Run(() => AddToScopeWithCleanup(scope, syncEvent, null));
            var task2 = Task.Run(() => AddToScopeWithCleanup(scope, syncEvent, null));

            syncEvent.Set();

            Task.WaitAll(task1, task2);

            scope.Dispose();

            Assert.Equal(DisposableItem.CreateCount, DisposableItem.DisposeCount);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Get disposal scope from http context
        /// </summary>
        /// <param name="locatorScope"></param>
        /// <returns></returns>
        public static IDisposalScope GetDisposalScopeFromHttpContext(IDisposalScope locatorScope)
        {
            if (HttpContext.Current == null ||
                !(locatorScope is DependencyInjectionContainer))
            {
                return(locatorScope);
            }

            var disposalScope = HttpContext.Current.Items[_disposalScopeKey] as IDisposalScope;

            if (disposalScope != null)
            {
                return(disposalScope);
            }

            disposalScope = new DisposalScope();

            HttpContext.Current.Items[_disposalScopeKey] = disposalScope;

            return(disposalScope);
        }
Exemplo n.º 7
0
        public void CleanUpDelegateTest()
        {
            DisposalScope disposalScope = new DisposalScope();
            DisposableService disposableService = new DisposableService();
            bool eventFired = false;
            bool cleanUpCalled = false;

            disposableService.Disposing += (sender, args) => eventFired = true;

            disposalScope.AddDisposable(disposableService,
                disposed =>
                {
                    Assert.True(ReferenceEquals(disposableService, disposed));

                    cleanUpCalled = true;
                });

            disposalScope.Dispose();

            Assert.True(eventFired);
            Assert.True(cleanUpCalled);
        }
Exemplo n.º 8
0
        public void CleanUpDelegateTest()
        {
            var disposalScope     = new DisposalScope();
            var disposableService = new DisposableService();
            var eventFired        = false;
            var cleanUpCalled     = false;

            disposableService.Disposing += (sender, args) => eventFired = true;

            disposalScope.AddDisposable(disposableService,
                                        disposed =>
            {
                Assert.True(ReferenceEquals(disposableService, disposed));

                cleanUpCalled = true;
            });

            disposalScope.Dispose();

            Assert.True(eventFired);
            Assert.True(cleanUpCalled);
        }
Exemplo n.º 9
0
        public void ThrowExceptionDuringCleanUp()
        {
            DisposalScope disposalScope = new DisposalScope();
            DisposableService disposableService = new DisposableService();
            bool eventFired = false;

            disposableService.Disposing += (sender, args) => eventFired = true;

            disposalScope.AddDisposable(disposableService, disposed => { throw new Exception(); });

            disposalScope.Dispose();

            Assert.True(eventFired);
        }
        /// <summary>
        /// Locate object
        /// </summary>
        /// <param name="creationDelegate"></param>
        /// <param name="injectionScope"></param>
        /// <param name="injectionContext"></param>
        /// <param name="exportStrategy"></param>
        /// <returns></returns>
        public object Locate(ExportActivationDelegate creationDelegate,
			IInjectionScope injectionScope,
			IInjectionContext injectionContext,
			IExportStrategy exportStrategy)
        {
            object scopeName = valueDelegate(injectionScope, injectionContext);
            Tuple<object, IDisposalScope> trackedObject;

            if (!scopedObjects.TryGetValue(scopeName, out trackedObject))
            {
                IDisposalScope disposalScope = injectionContext.DisposalScope;
                IInjectionScope requestingScope = injectionContext.RequestingScope;

                IDisposalScope newDisposalScope = new DisposalScope();

                injectionContext.DisposalScope = newDisposalScope;
                injectionContext.RequestingScope = exportStrategy.OwningScope;

                object locatedObject = creationDelegate(injectionScope, injectionContext);

                injectionContext.DisposalScope = disposalScope;
                injectionContext.RequestingScope = requestingScope;

                if (locatedObject != null)
                {
                    trackedObject = new Tuple<object, IDisposalScope>(locatedObject, newDisposalScope);

                    scopedObjects[scopeName] = trackedObject;

                    INotifyWhenDisposed notifyWhenDisposed = scopeName as INotifyWhenDisposed;

                    if (notifyWhenDisposed != null)
                    {
                        notifyWhenDisposed.Disposed += (sender, args) =>
                                                       {
                                                           scopedObjects.Remove(scopeName);
                                                                     newDisposalScope.Dispose();
                                                       };
                    }

                    return locatedObject;
                }
            }
            else
            {
                return trackedObject.Item1;
            }

            return null;
        }
Exemplo n.º 11
0
		/// <summary>
		/// Default constructor
		/// </summary>
		/// <param name="requestingScope">requesting scope</param>
		public DisposableInjectionContext(IInjectionScope requestingScope) : base(requestingScope)
		{
			DisposalScope = new DisposalScope();
		}