Example #1
0
        public void RequestLifetimeManagerReturnsSameObjectForSameRequest()
        {
            var context1 = new FakeHttpContext("Http://fakeUrl1.com");
            var context2 = new FakeHttpContext("Http://fakeUrl2.com");

            var requestltm = new RequestLifetime();

            var container = new Container();

            container.Register <IFoo>(c => new Foo1())
            .WithLifetimeManager(requestltm);

            requestltm.SetContext(context1);

            var result1 = container.Resolve <IFoo>();
            var result2 = container.Resolve <IFoo>();

            requestltm.SetContext(context2);

            var result3 = container.Resolve <IFoo>();

            Assert.IsNotNull(result1);
            Assert.IsNotNull(result2);
            Assert.IsNotNull(result3);

            Assert.AreSame(result1, result2);       // same request
            Assert.AreNotSame(result2, result3);    // different request
        }
        public void RequestLifetimeNestedWorksWithCompile()
        {
            // This doesnt work - will affect other Lifetimes that just nests the expression
            // Need to fix the scope of the input parameter IResolver

            using (var container = new IocContainer())
            {
                // Arrange
                var context         = new FakeHttpContext("Http://fakeUrl1.com");
                var requestLifetime = new RequestLifetime(() => context, disposeOnEnd: false);                  // Do not test disposeOnEnd here

                container.Register <IFoo, Foo1>();
                container.Register <IBar, Bar1>().SetLifetime(requestLifetime);
                container.Register <IFooBar, FooBar>();
                container.Register <IFooBarContainer, FooBarContainer>();

                // Act
                container.Compile();

                var fooBarContainer = container.Resolve <IFooBarContainer>();

                // Assert
                Assert.IsNotNull(fooBarContainer);
                Assert.IsNotNull(fooBarContainer.FooBar);
                Assert.IsNotNull(fooBarContainer.FooBar.Foo);
                Assert.IsNotNull(fooBarContainer.FooBar.Bar);
            }
        }
        public void RequestLifetimeReturnsSameInstanceForSameRequest()
        {
            // Arrange
            using (var container = new IocContainer())
            {
                var context1 = new FakeHttpContext("Http://fakeUrl1.com");
                var context2 = new FakeHttpContext("Http://fakeUrl2.com");

                HttpContextBase currentContext = null;

                var lifetime = new RequestLifetime(() => currentContext);                               // better solution for injecting HttpContext ?

                container.Register <IFoo>(c => new Foo1()).SetLifetime(lifetime);

                // Act
                currentContext = context1;

                var result1 = container.Resolve <IFoo>();
                var result2 = container.Resolve <IFoo>();

                currentContext = context2;

                var result3 = container.Resolve <IFoo>();

                // Assert
                Assert.IsNotNull(result1);
                Assert.IsNotNull(result2);
                Assert.IsNotNull(result3);

                Assert.AreSame(result1, result2);
                Assert.AreNotSame(result1, result3);
            }
        }
        public void RequestLifetimeManagerReturnsSameObjectForSameRequest()
        {
            var context1 = new FakeHttpContext("Http://fakeUrl1.com");
            var context2 = new FakeHttpContext("Http://fakeUrl2.com");

            var requestltm = new RequestLifetime();

            var container = new IocContainer();

            container.Register <IFoo>(c => new Foo1())
            .WithLifetimeManager(requestltm);

            requestltm.SetContext(context1);

            var result1 = container.Resolve <IFoo>();
            var result2 = container.Resolve <IFoo>();

            requestltm.SetContext(context2);

            var result3 = container.Resolve <IFoo>();

            Verify.That(result3).IsNotNull();
            Verify.That(result2).IsNotNull();
            Verify.That(result1).IsNotNull()
            .IsTheSameObjectAs(result2)
            .IsNotTheSameObjectAs(result3);
        }
Example #5
0
        public void RequestLifetimeReturnsSameInstanceForSameRequest()
        {
            // Arrange
            using (var container = new IocContainer())
            {
                var context1 = new FakeHttpContext("Http://fakeUrl1.com");
                var context2 = new FakeHttpContext("Http://fakeUrl2.com");

                HttpContextBase currentContext = null;

                var lifetime = new RequestLifetime(() => currentContext);		// better solution for injecting HttpContext ?

                container.Register<IFoo>(c => new Foo1()).SetLifetime(lifetime);

                // Act
                currentContext = context1;

                var result1 = container.Resolve<IFoo>();
                var result2 = container.Resolve<IFoo>();

                currentContext = context2;

                var result3 = container.Resolve<IFoo>();

                // Assert
                Assert.IsNotNull(result1);
                Assert.IsNotNull(result2);
                Assert.IsNotNull(result3);

                Assert.AreSame(result1, result2);
                Assert.AreNotSame(result1, result3);
            }
        }
Example #6
0
        private Task ProcessMessages(ITransportConnection connection, Func <Task> initialize)
        {
            var disposer = new Disposer();

            var cancelContext = new LongPollingTransportContext(this, disposer);

            // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
            IDisposable registration = ConnectionEndToken.SafeRegister(state => Cancel(state), cancelContext);

            var lifeTime       = new RequestLifetime(this, _requestLifeTime, registration);
            var messageContext = new MessageContext(this, lifeTime);

            try
            {
                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                IDisposable subscription = connection.Receive(MessageId,
                                                              (response, state) => OnMessageReceived(response, state),
                                                              MaxMessages,
                                                              messageContext);

                // Set the disposable
                disposer.Set(subscription);

                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                initialize().Catch((ex, state) => OnError(ex, state), messageContext);
            }
            catch (Exception ex)
            {
                lifeTime.Complete(ex);
            }

            return(_requestLifeTime.Task);
        }
        public void CanSetDefaultLifetimeManagerToRequestLifetime()
        {
            var lifetime = new RequestLifetime();

            iocContainer.UsesDefaultLifetimeManagerOf(lifetime);

            Verify.That(iocContainer.DefaultLifetimeManager).IsTheSameObjectAs(lifetime);
        }
Example #8
0
        protected override void InitializePersistentState()
        {
            base.InitializePersistentState();

            // The _transportLifetime must be initialized after calling base.InitializePersistentState since
            // _transportLifetime depends on _requestLifetime.
            _transportLifetime = new RequestLifetime(this, _requestLifeTime);
        }
        protected override async Task InitializePersistentState()
        {
            await base.InitializePersistentState().PreserveCulture();

            // The _transportLifetime must be initialized after calling base.InitializePersistentState since
            // _transportLifetime depends on _requestLifetime.
            _transportLifetime = new RequestLifetime(this, _requestLifeTime);
        }
        protected override async Task InitializePersistentState()
        {
            await base.InitializePersistentState().PreserveCulture();

            // The _transportLifetime must be initialized after calling base.InitializePersistentState since
            // _transportLifetime depends on _requestLifetime.
            _transportLifetime = new RequestLifetime(this, _requestLifeTime);
        }
Example #11
0
        private Task ProcessMessages(ITransportConnection connection, Func <Task> initialize)
        {
            var disposer = new Disposer();

            if (BeforeCancellationTokenCallbackRegistered != null)
            {
                BeforeCancellationTokenCallbackRegistered();
            }

            var cancelContext = new ForeverTransportContext(this, disposer);

            // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
            IDisposable registration = ConnectionEndToken.SafeRegister(state => Cancel(state), cancelContext);

            var lifetime       = new RequestLifetime(this, _requestLifeTime);
            var messageContext = new MessageContext(this, lifetime, registration);

            if (BeforeReceive != null)
            {
                BeforeReceive();
            }

            try
            {
                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                IDisposable subscription = connection.Receive(LastMessageId,
                                                              (response, state) => OnMessageReceived(response, state),
                                                              MaxMessages,
                                                              messageContext);


                disposer.Set(subscription);

                if (AfterReceive != null)
                {
                    AfterReceive();
                }

                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                initialize().Then(tcs => tcs.TrySetResult(null), InitializeTcs)
                .Catch((ex, state) => OnError(ex, state), messageContext);
            }
            catch (OperationCanceledException ex)
            {
                InitializeTcs.TrySetCanceled();

                lifetime.Complete(ex);
            }
            catch (Exception ex)
            {
                InitializeTcs.TrySetCanceled();

                lifetime.Complete(ex);
            }

            return(_requestLifeTime.Task);
        }
Example #12
0
        protected override void InitializePersistentState()
        {
            // PersistentConnection.OnConnected must complete before we can write to the output stream,
            // so clients don't indicate the connection has started too early.
            InitializeTcs = new TaskCompletionSource <object>();

            // WriteQueue must be reinitialized before calling base.InitializePersistentState to ensure
            // _requestLifeTime will be properly initialized.
            WriteQueue = new TaskQueue(InitializeTcs.Task);

            base.InitializePersistentState();

            // The _transportLifetime must be initialized after calling base.InitializePersistentState since
            // _transportLifetime depends on _requestLifetime.
            _transportLifetime = new RequestLifetime(this, _requestLifeTime);
        }
Example #13
0
        public void RequestLifetimeWithDisposeOnEndThrowsExceptionIfInstanceIsNotIDisposable()
        {
            // Currently throws the wrong Expection (InvalidCastException)

            using (var container = new IocContainer())
            {
                // Arrange
                var context = new FakeHttpContext("Http://fakeUrl1.com");
                var lifetime = new RequestLifetime(() => context, disposeOnEnd: true);

                // Should throw exception here
                container.Register<IFoo>(c => new Foo1()).SetLifetime(lifetime);

                // But currently first throws exception here
                var instance = container.Resolve<IFoo>();
            }
        }
		public void RequestLifetimeWithDisposeOnEndThrowsExceptionIfInstanceIsNotIDisposable()
		{
			// What exception should it throw?
			// Type Exception of some sort ?

			using (var container = new IocContainer())
			{
				// Arrange
				var context = new FakeHttpContext("Http://fakeUrl1.com");
				var lifetime = new RequestLifetime(() => context, disposeOnEnd: true);

				// Should throw exception here
				container.Register<IFoo>(c => new Foo1()).SetLifetime(lifetime);

				// But currently first throws exception here
				var instance = container.Resolve<IFoo>();
			}
		}
        public void RequestLifetimeWithDisposeOnEndThrowsExceptionIfInstanceIsNotIDisposable()
        {
            // What exception should it throw?
            // Type Exception of some sort ?

            using (var container = new IocContainer())
            {
                // Arrange
                var context  = new FakeHttpContext("Http://fakeUrl1.com");
                var lifetime = new RequestLifetime(() => context, disposeOnEnd: true);

                // Should throw exception here
                container.Register <IFoo>(c => new Foo1()).SetLifetime(lifetime);

                // But currently first throws exception here
                var instance = container.Resolve <IFoo>();
            }
        }
        public void RequestLifetimeWorksWithCompile()
        {
            using (var container = new IocContainer())
            {
                // Arrange
                var context         = new FakeHttpContext("Http://fakeUrl1.com");
                var requestLifetime = new RequestLifetime(() => context, disposeOnEnd: false);                  // Do not test disposeOnEnd here

                container.Register <IFoo, Foo1>();
                container.Register <IBar, Bar1>().SetLifetime(requestLifetime);
                container.Register <IFooBar, FooBar>();

                // Act
                container.Compile();

                var fooBar = container.Resolve <IFooBar>();

                // Assert
                Assert.IsNotNull(fooBar);
                Assert.IsNotNull(fooBar.Foo);
                Assert.IsNotNull(fooBar.Bar);
            }
        }
Example #17
0
 public MessageContext(LongPollingTransport longPollingTransport, RequestLifetime requestLifetime)
 {
     Transport = longPollingTransport;
     Lifetime  = requestLifetime;
 }
Example #18
0
        protected override void InitializePersistentState()
        {
            base.InitializePersistentState();

            // The _transportLifetime must be initialized after calling base.InitializePersistentState since
            // _transportLifetime depends on _requestLifetime.
            _transportLifetime = new RequestLifetime(this, _requestLifeTime);
        }
 public void EndRequestLifetime()
 {
     RequestLifetime.Dispose();
 }
Example #20
0
 public MessageContext(ForeverTransport transport, RequestLifetime lifetime, IDisposable registration)
 {
     Registration = registration;
     Lifetime     = lifetime;
     Transport    = transport;
 }
		public void RequestLifetimeNestedWorksWithCompile()
		{
			// This doesnt work - will affect other Lifetimes that just nests the expression 
			// Need to fix the scope of the input parameter IResolver

			using (var container = new IocContainer())
			{
				// Arrange
				var context = new FakeHttpContext("Http://fakeUrl1.com");
				var requestLifetime = new RequestLifetime(() => context, disposeOnEnd: false);	// Do not test disposeOnEnd here

				container.Register<IFoo, Foo1>();
				container.Register<IBar, Bar1>().SetLifetime(requestLifetime);
				container.Register<IFooBar, FooBar>();
				container.Register<IFooBarContainer, FooBarContainer>();

				// Act
				container.Compile();

				var fooBarContainer = container.Resolve<IFooBarContainer>();

				// Assert
				Assert.IsNotNull(fooBarContainer);
				Assert.IsNotNull(fooBarContainer.FooBar);
				Assert.IsNotNull(fooBarContainer.FooBar.Foo);
				Assert.IsNotNull(fooBarContainer.FooBar.Bar);
			}
		}
		public void RequestLifetimeWorksWithCompile()
		{
			using (var container = new IocContainer())
			{
				// Arrange
				var context = new FakeHttpContext("Http://fakeUrl1.com");
				var requestLifetime = new RequestLifetime(() => context, disposeOnEnd: false);	// Do not test disposeOnEnd here

				container.Register<IFoo, Foo1>();
				container.Register<IBar, Bar1>().SetLifetime(requestLifetime);
				container.Register<IFooBar, FooBar>();

				// Act
				container.Compile();

				var fooBar = container.Resolve<IFooBar>();

				// Assert
				Assert.IsNotNull(fooBar);
				Assert.IsNotNull(fooBar.Foo);
				Assert.IsNotNull(fooBar.Bar);
			}
		}
        protected override void InitializePersistentState()
        {
            // PersistentConnection.OnConnected must complete before we can write to the output stream,
            // so clients don't indicate the connection has started too early.
            InitializeTcs = new TaskCompletionSource<object>();

            // WriteQueue must be reinitialized before calling base.InitializePersistentState to ensure
            // _requestLifeTime will be properly initialized.
            WriteQueue = new TaskQueue(InitializeTcs.Task);

            base.InitializePersistentState();

            // The _transportLifetime must be initialized after calling base.InitializePersistentState since
            // _transportLifetime depends on _requestLifetime.
            _transportLifetime = new RequestLifetime(this, _requestLifeTime);
        }
 public MessageContext(ForeverTransport transport, RequestLifetime lifetime, IDisposable registration)
 {
     Registration = registration;
     Lifetime = lifetime;
     Transport = transport;
 }
        private Task ProcessMessages(ITransportConnection connection, Func<Task> initialize)
        {
            var disposer = new Disposer();

            if (BeforeCancellationTokenCallbackRegistered != null)
            {
                BeforeCancellationTokenCallbackRegistered();
            }

            var cancelContext = new ForeverTransportContext(this, disposer);

            // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
            IDisposable registration = ConnectionEndToken.SafeRegister(state => Cancel(state), cancelContext);

            var lifetime = new RequestLifetime(this, _requestLifeTime);
            var messageContext = new MessageContext(this, lifetime, registration);

            if (BeforeReceive != null)
            {
                BeforeReceive();
            }

            try
            {
                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                IDisposable subscription = connection.Receive(LastMessageId,
                                                              (response, state) => OnMessageReceived(response, state),
                                                               MaxMessages,
                                                               messageContext);

                disposer.Set(subscription);

                if (AfterReceive != null)
                {
                    AfterReceive();
                }

                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                initialize().Then(tcs => tcs.TrySetResult(null), InitializeTcs)
                            .Catch((ex, state) => OnError(ex, state), messageContext);
            }
            catch (OperationCanceledException ex)
            {
                InitializeTcs.TrySetCanceled();

                lifetime.Complete(ex);
            }
            catch (Exception ex)
            {
                InitializeTcs.TrySetCanceled();

                lifetime.Complete(ex);
            }

            return _requestLifeTime.Task;
        }
 public MessageContext(LongPollingTransport longPollingTransport, RequestLifetime requestLifetime)
 {
     Transport = longPollingTransport;
     Lifetime = requestLifetime;
 }
        private Task ProcessMessages(ITransportConnection connection, Func<Task> initialize)
        {
            var disposer = new Disposer();

            var cancelContext = new LongPollingTransportContext(this, disposer);

            // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
            IDisposable registration = ConnectionEndToken.SafeRegister(state => Cancel(state), cancelContext);

            var lifeTime = new RequestLifetime(this, _requestLifeTime, registration);
            var messageContext = new MessageContext(this, lifeTime);

            try
            {
                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                IDisposable subscription = connection.Receive(MessageId,
                                                              (response, state) => OnMessageReceived(response, state),
                                                              MaxMessages,
                                                              messageContext);

                // Set the disposable
                disposer.Set(subscription);

                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                initialize().Catch((ex, state) => OnError(ex, state), messageContext);
            }
            catch (Exception ex)
            {
                lifeTime.Complete(ex);
            }

            return _requestLifeTime.Task;
        }