示例#1
0
        public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
        {
            var routeData = request.GetRouteData().Values;

            if (routeData.ContainsKey("ServiceName") && routeData.ContainsKey("ActionName"))
            {
                var serviceName = routeData["ServiceName"].ToString();
                var actionName  = routeData["ActionName"].ToString();

                if (DynamicHttpControllerManager.GetInstance().ContainsService(serviceName))
                {
                    var controllerInfo = DynamicHttpControllerManager.GetInstance().GetControllerInfo(serviceName);
                    var controller     = DynamicHttpControllerManager.GetInstance().Resolve(serviceName);
                    if (controller == null)
                    {
                        return(base.SelectController(request));
                    }

                    var controllerDescriptor = new DynamicHttpControllerDescriptor(_configuration, serviceName, controllerInfo.ControllerType);
                    controllerDescriptor.Properties["ServiceName"]         = serviceName;
                    controllerDescriptor.Properties["ActionName"]          = actionName;
                    controllerDescriptor.Properties["IsDynamicController"] = true;
                    controllerDescriptor.Properties["ServiceType"]         = controllerInfo.ServiceType;
                    controllerDescriptor.Properties["ControllerType"]      = controller.GetType();
                    return(controllerDescriptor);
                }
            }

            return(base.SelectController(request));
        }
        public static DynamicHttpControllerManager GetInstance()
        {
            if (_instance == null)
            {
                lock (_lockObj)
                {
                    if (_instance == null)
                    {
                        _instance = new DynamicHttpControllerManager();
                    }

                    return(_instance);
                }
            }

            return(_instance);
        }
示例#3
0
        void Application_Start(object sender, EventArgs e)
        {
            // 注册自定义工厂
            var container = new Castle.Windsor.WindsorContainer();

            //通过Castle组合BaseController和ICalculator接口
            DynamicHttpControllerManager.GetInstance().RegisterType <CalculatorService, ICalculator>();
            DynamicHttpControllerManager.GetInstance().RegisterType <MessageService, IMessage>();
            //container.Register(
            //    // 注册ICalculator
            //    Component.For<CalculatorService, ICalculator>(),
            //    Component.For<DynamicApiInterceptor<ICalculator>>().LifestyleTransient(),
            //    Component.For<BaseController>().Proxy.AdditionalInterfaces(typeof(ICalculator))
            //        .Interceptors<DynamicApiInterceptor<ICalculator>>().LifestyleTransient()
            //        .Named("Calculator"),

            //    //注册IMessage
            //    Component.For<MessageService, IMessage>(),
            //    Component.For<DynamicApiInterceptor<IMessage>>().LifestyleTransient(),
            //    Component.For<BaseController>().Proxy.AdditionalInterfaces(typeof(IMessage))
            //        .Interceptors<DynamicApiInterceptor<IMessage>>().LifestyleTransient()
            //        .Named("Message")

            //);

            //var dynamicControllerFactory = new DynamicControllerFactory(container);
            //ControllerBuilder.Current.SetControllerFactory(dynamicControllerFactory);

            var configuration                 = GlobalConfiguration.Configuration;
            var dynamicControllerSelector     = new DynamicHttpControllerSelector(configuration);
            var dynamicHttpControllerActivtor = new DynamicHttpControllerActivtor();
            var dynamicActionSelector         = new DynamicHttpActionSelector();

            GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), dynamicControllerSelector);
            GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpActionSelector), dynamicActionSelector);
            GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), dynamicHttpControllerActivtor);

            // 在应用程序启动时运行的代码
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
 public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
 {
     return((IHttpController)DynamicHttpControllerManager.GetInstance().Resolve(controllerType));
 }