Beispiel #1
0
        internal void SetSessionStateMode(HttpContextBase context, IDynamicSessionStateConfigurator configurator)
        {
            MvcHandler mvcHandler = context.Handler as MvcHandler;

            if (mvcHandler == null)
            {
                // Either MvcHttpHandler was called directly, Routing hasn't run, or Routing has run
                // and the chosen handler isn't MVC. There's nothing we can do here.
                return;
            }

            // Check to see that our factory is installed, otherwise the controller factory might be asked to
            // create two instances of the controller for every request, which could lead to resource or
            // scalability issues.
            RequestContext requestContext = mvcHandler.RequestContext;
            MvcDynamicSessionControllerFactory factory = ControllerBuilder.GetControllerFactory() as MvcDynamicSessionControllerFactory;

            if (factory == null)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture,
                                                                  MvcResources.MvcDynamicSessionModule_WrongControllerFactory, typeof(MvcDynamicSessionControllerFactory)));
            }

            context.Items[_controllerFactoryItemKey] = factory; // save a reference to this factory so that we can dispose of the cached controller
            string                 controllerName   = requestContext.RouteData.GetRequiredString("controller");
            IController            controller       = factory.CreateCachedController(requestContext, controllerName);
            ControllerSessionState sessionStateMode = GetSessionStateMode(controller);

            configurator.ConfigureSessionState(sessionStateMode);
        }
Beispiel #2
0
        internal static void PossiblyReleaseController(HttpContextBase context)
        {
            // It's possible that the factory can create the cached controller but that the MVC pipeline doesn't run
            // or doesn't make it as far as the CreateController() call. In this case, we need to release the cached
            // controller manually in case the controller is a pooled resource.

            MvcDynamicSessionControllerFactory factory = context.Items[_controllerFactoryItemKey] as MvcDynamicSessionControllerFactory;

            if (factory != null)
            {
                factory.ReleaseCachedController(context);
            }
        }
Beispiel #3
0
        public static void Initialize()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());            

            var host = new MultiGameHost();            
            builder.RegisterInstance(host)
                .As<MultiGameHost>();

            _container = builder.Build();

            var factory =
                new MvcDynamicSessionControllerFactory(new AutofacControllerFactory(new ContainerProvider(_container)));
            ControllerBuilder.Current.SetControllerFactory(factory);
        }
        public void CreateCachedController_UnderlyingFactoryReturnsNull() {
            // Arrange
            RequestContext requestContext = GetRequestContext();

            Mock<IControllerFactory> mockUnderlyingFactory = new Mock<IControllerFactory>();
            mockUnderlyingFactory.Expect(o => o.CreateController(requestContext, "home")).Returns((IController)null).AtMostOnce();
            MvcDynamicSessionControllerFactory factory = new MvcDynamicSessionControllerFactory(mockUnderlyingFactory.Object);

            // Act
            IController controller1 = factory.CreateCachedController(requestContext, "home");
            IController controller2 = factory.CreateController(requestContext, "home");

            // Assert
            Assert.IsNull(controller1);
            Assert.IsNull(controller2);
            mockUnderlyingFactory.Verify();
        }
        public void CreateController_RemovesCachedController() {
            // Arrange
            RequestContext requestContext = GetRequestContext();
            EmptyController controller = new EmptyController();
            int numTimesCalled = 0;

            Mock<IControllerFactory> mockUnderlyingFactory = new Mock<IControllerFactory>();
            mockUnderlyingFactory.Expect(o => o.CreateController(requestContext, "home")).Returns(controller).Callback(() => { numTimesCalled++; });
            MvcDynamicSessionControllerFactory factory = new MvcDynamicSessionControllerFactory(mockUnderlyingFactory.Object);

            // Act & assert 1
            IController controller1 = factory.CreateCachedController(requestContext, "home");
            IController controller2 = factory.CreateController(requestContext, "home");
            Assert.AreEqual(1, numTimesCalled);

            // Act & assert 2
            IController controller3 = factory.CreateController(requestContext, "home");
            Assert.AreEqual(2, numTimesCalled);
        }
        public void ReleaseCachedController_ControllerInstanceCached_ReleasesInstance() {
            // Arrange
            RequestContext requestContext = GetRequestContext();
            EmptyController controller = new EmptyController();

            Mock<IControllerFactory> mockUnderlyingFactory = new Mock<IControllerFactory>();
            mockUnderlyingFactory.Expect(o => o.CreateController(requestContext, "home")).Returns(controller).AtMostOnce();
            mockUnderlyingFactory.Expect(o => o.ReleaseController(controller)).Verifiable();
            MvcDynamicSessionControllerFactory factory = new MvcDynamicSessionControllerFactory(mockUnderlyingFactory.Object);

            // Act
            factory.CreateCachedController(requestContext, "home");
            factory.ReleaseCachedController(requestContext.HttpContext);

            // Assert
            mockUnderlyingFactory.Verify();
        }
        public void CreateController_ThrowsIfRequestContextIsNull() {
            // Arrange
            MvcDynamicSessionControllerFactory factory = new MvcDynamicSessionControllerFactory();

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                    factory.CreateController(null, "home");
                }, "requestContext");
        }
        public void ReleaseController_ForwardsToUnderlyingFactory() {
            // Arrange
            EmptyController controller = new EmptyController();

            Mock<IControllerFactory> mockUnderlyingFactory = new Mock<IControllerFactory>();
            mockUnderlyingFactory.Expect(o => o.ReleaseController(controller)).Verifiable();
            MvcDynamicSessionControllerFactory factory = new MvcDynamicSessionControllerFactory(mockUnderlyingFactory.Object);

            // Act
            factory.ReleaseController(controller);

            // Assert
            mockUnderlyingFactory.Verify();
        }
        public void ReleaseCachedController_ControllerInstanceNotCached_DoesNothing() {
            // Arrange
            RequestContext requestContext = GetRequestContext();

            Mock<IControllerFactory> mockUnderlyingFactory = new Mock<IControllerFactory>();
            mockUnderlyingFactory.Expect(o => o.ReleaseController(It.IsAny<IController>())).Never();
            MvcDynamicSessionControllerFactory factory = new MvcDynamicSessionControllerFactory(mockUnderlyingFactory.Object);

            // Act
            factory.ReleaseCachedController(requestContext.HttpContext);

            // Assert
            // If we got this far, success!
        }