public void CreateController_UsesControllerActivatorToInstantiateController() { // Arrange var expected = new MyController(); var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(MyController).GetTypeInfo() }; var httpContext = new DefaultHttpContext(); httpContext.RequestServices = GetServices(); var actionContext = new ActionContext(httpContext, new RouteData(), actionDescriptor); var activator = new Mock<IControllerActivator>(); activator.Setup(a => a.Create(actionContext, typeof(MyController))) .Returns(expected) .Verifiable(); var controllerFactory = new DefaultControllerFactory(activator.Object); // Act var result = controllerFactory.CreateController(actionContext); // Assert var controller = Assert.IsType<MyController>(result); Assert.Same(expected, controller); activator.Verify(); }
public IController CreateController(RequestContext requestContext, string controllerName) { if (controllerName.ToLower().StartsWith("flight")) { var Flightrepository = new FlightRepository(); var service = new FlightServices(Flightrepository); var controller = new FlightController(service); return(controller); } var defaultFactory = new DefaultControllerFactory(); return(defaultFactory.CreateController(requestContext, controllerName)); }
public override IController CreateController(RequestContext requestContext, string controllerName) { try { string controllername = requestContext.RouteData.Values["controller"].ToString(); Type controllerType = Type.GetType(string.Format("IoC_Container_Practice.Controllers.{0}Controller", controllername), false, true); IController controller = _container.Resolve(controllerType) as IController; return(controller); } catch { var defaultFactory = new DefaultControllerFactory(); return(defaultFactory.CreateController(requestContext, controllerName)); } }
public IController CreateController(RequestContext requestContext, string controllerName) { if (controllerName.StartsWith("Home")) { Container container = new Container(); container.Register <IService1, IoCService1>(IoCApplication.Models.CycleTimeOptions.transient); container.Register <IService2, IoCService2>(IoCApplication.Models.CycleTimeOptions.singleton); IService1 service1 = container.Resolve <IService1>(); IService2 service2 = container.Resolve <IService2>(); IController controller = new HomeController(service1, service2); return(controller); } var defaultFactory = new DefaultControllerFactory(); return(defaultFactory.CreateController(requestContext, controllerName)); }
public void CreateController_ThrowsIfActionDescriptorIsNotControllerActionDescriptor() { // Arrange var expected = "The action descriptor must be of type 'Microsoft.AspNet.Mvc.ControllerActionDescriptor'." + Environment.NewLine + "Parameter name: actionContext"; var actionDescriptor = new ActionDescriptor(); var controllerFactory = new DefaultControllerFactory(Mock.Of<IControllerActivator>()); var httpContext = new DefaultHttpContext(); var actionContext = new ActionContext(httpContext, new RouteData(), actionDescriptor); // Act and Assert var ex = Assert.Throws<ArgumentException>(() => controllerFactory.CreateController(actionContext)); Assert.Equal(expected, ex.Message); Assert.Equal("actionContext", ex.ParamName); }
public IController CreateController(RequestContext requestContext, string controllerName) { if (controllerName.ToLower().StartsWith("proteintracker")) { //Create an instance of the ProteinTrackingService var service = new ProteinTrackingService(); //Pass this new instance into the creation of the controller. Now remember, that even though this is a concrete instance //the controller can accept anything derived from IProteinTrackingService //It doesn't care what the class is, as long as it's implementing this interface var controller = new ProteinTrackerController(service); return(controller); } //For all other types of Controller, we're just going to let it do what the default is var defaultController = new DefaultControllerFactory(); return(defaultController.CreateController(requestContext, controllerName)); }
public void CreateController_ThrowsIfActionDescriptorIsNotControllerActionDescriptor() { // Arrange var expected = "The action descriptor must be of type 'Microsoft.AspNet.Mvc.ControllerActionDescriptor'." + Environment.NewLine + "Parameter name: actionContext"; var actionDescriptor = new ActionDescriptor(); var controllerFactory = new DefaultControllerFactory(Mock.Of <IControllerActivator>()); var httpContext = new DefaultHttpContext(); var actionContext = new ActionContext(httpContext, new RouteData(), actionDescriptor); // Act and Assert var ex = Assert.Throws <ArgumentException>(() => controllerFactory.CreateController(actionContext)); Assert.Equal(expected, ex.Message); Assert.Equal("actionContext", ex.ParamName); }
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName) { if (controllerName.ToLower().StartsWith("emp")) { var db = new DepartmentDb(); var controller = new EmployeeController(db); return(controller); } if (controllerName.ToLower().StartsWith("hom")) { var db = new DepartmentDb(); var controller = new HomeController(db); return(controller); } var defaultFactory = new DefaultControllerFactory(); return(defaultFactory.CreateController(requestContext, controllerName)); }
public void CreateController_ThrowsIfPropertyCannotBeActivated() { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(ControllerThatCannotBeActivated).GetTypeInfo() }; var services = GetServices(); var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act and Assert var exception = Assert.Throws <InvalidOperationException>(() => factory.CreateController(context)); Assert.Equal("The property 'Service' on controller '" + typeof(ControllerThatCannotBeActivated) + "' cannot be activated.", exception.Message); }
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName) { //get spring context WebApplicationContext ctx = ContextRegistry.GetContext() as WebApplicationContext; string controller = controllerName + "Controller"; //查找是否配置该Controller if (ctx.ContainsObject(controller)) { object controllerf = ctx.GetObject(controller); return((IController)controllerf); } else { if (defalutf == null) { defalutf = new DefaultControllerFactory(); } return(defalutf.CreateController(requestContext, controllerName)); } }
public object CreateController(ControllerContext context) { var cronusContext = context.HttpContext.RequestServices.GetRequiredService <CronusContext>(); if (cronusContext.IsNotInitialized) { var tenant = options.TenantResolver?.Resolve(context.HttpContext); if (string.IsNullOrEmpty(tenant)) { tenant = options.DefaultTenant; } if (string.IsNullOrEmpty(tenant)) { throw new Exception("Unable to resolve tenant. Make sure that you have `IAspNetTenantResolver` registered. The default implementation is ResolveTenantFromTenantClaim. Another way to hack it is to use `.AddCronusAspNetCore(o => o.DefaultTenant = \"myTenant\")"); } cronusContext.Initialize(tenant, context.HttpContext.RequestServices); } return(defaultControllerFactory.CreateController(context)); }
public void CreateController_SetsPropertiesFromActionContextHierarchy() { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(ControllerWithAttributes).GetTypeInfo() }; var services = GetServices(); var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act var result = factory.CreateController(context); // Assert var controller = Assert.IsType <ControllerWithAttributes>(result); Assert.Same(context, controller.ActionContext); }
public void CreateController_ThrowsIfControllerCannotBeActivated(Type type) { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = type.GetTypeInfo() }; var services = GetServices(); var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act and Assert var exception = Assert.Throws <InvalidOperationException>(() => factory.CreateController(context)); Assert.Equal( $"The type '{type.FullName}' cannot be activated by '{typeof(DefaultControllerFactory).FullName}' " + "because it is either a value type, an interface, an abstract class or an open generic type.", exception.Message); }
public void CreateController_IgnoresPropertiesThatAreNotDecoratedWithActivateAttribute() { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(ControllerWithActivateAndFromServices).GetTypeInfo() }; var services = GetServices(); var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act var result = factory.CreateController(context); // Assert var controller = Assert.IsType <ControllerWithActivateAndFromServices>(result); Assert.Null(controller.Response); }
public void CreateController_SetsPropertiesFromActionContextHierarchy() { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(ControllerWithActivateAndFromServices).GetTypeInfo() }; var services = GetServices(); var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act var result = factory.CreateController(context); // Assert var controller = Assert.IsType<ControllerWithActivateAndFromServices>(result); Assert.Same(context, controller.ActionContext); Assert.Same(httpContext, controller.HttpContext); }
public void CreateController_ThrowsIfPropertyCannotBeActivated() { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(ControllerThatCannotBeActivated).GetTypeInfo() }; var services = GetServices(); var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act and Assert var exception = Assert.Throws <InvalidOperationException>(() => factory.CreateController(context)); Assert.Equal( $"Unable to resolve service for type '{typeof(TestService).FullName}' while attempting to activate " + $"'{typeof(ControllerThatCannotBeActivated).FullName}'.", exception.Message); }
public void CreateController_SetsViewDataDictionary() { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(ControllerWithActivateAndFromServices).GetTypeInfo() }; var services = GetServices(); var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act var result = factory.CreateController(context); // Assert var controller = Assert.IsType <ControllerWithActivateAndFromServices>(result); Assert.NotNull(controller.GetViewData()); }
public void CreateController_IgnoresPropertiesThatAreNotDecoratedWithActivateAttribute() { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(ControllerWithActivateAndFromServices).GetTypeInfo() }; var services = GetServices(); var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act var result = factory.CreateController(context); // Assert var controller = Assert.IsType<ControllerWithActivateAndFromServices>(result); Assert.Null(controller.Response); }
public void CreateController_ThrowsIfPropertyCannotBeActivated() { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(ControllerThatCannotBeActivated).GetTypeInfo() }; var services = GetServices(); var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act and Assert var exception = Assert.Throws<InvalidOperationException>(() => factory.CreateController(context)); Assert.Equal("The property 'Service' on controller '" + typeof(ControllerThatCannotBeActivated) + "' cannot be activated.", exception.Message); }
public IController CreateController(RequestContext requestContext, string controllerName) { return(defaultControllerFactory.CreateController(requestContext, controllerName)); }
public void CreateController_SetsBindingContext() { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(ControllerWithActivateAndFromServices).GetTypeInfo() }; var bindingContext = new ActionBindingContext(); var services = GetServices(); services.GetRequiredService<IScopedInstance<ActionBindingContext>>().Value = bindingContext; var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act var result = factory.CreateController(context); // Assert var controller = Assert.IsType<ControllerWithActivateAndFromServices>(result); Assert.Same(bindingContext, controller.BindingContext); }