/// <summary>
        /// 创建IHttpController
        /// </summary>
        /// <param name="request"></param>
        /// <param name="controllerDescriptor"></param>
        /// <param name="controllerType"></param>
        /// <returns></returns>
        public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
        {
            if (request == null)
            {
                throw new Exception("request");
            }

            //获取没有使用IOC注入的控制器
            if (!BlackList.Contains(controllerDescriptor.ControllerName))
            {
                //结合Unity容器创建,注意Unity容器名称规定为"{控制器名称}+api"形式
                IUnityContainer unityContainer = new IocHelper(controllerDescriptor.ControllerName + "Api").GetUnityContainer();
                if (unityContainer != null)
                {
                    IHttpController controller = (IHttpController)unityContainer.Resolve(controllerType);
                    return(controller);
                }
            }

            return(new DefaultHttpControllerActivator().Create(request, controllerDescriptor, controllerType));
        }
Esempio n. 2
0
        public override IHttpController CreateController(HttpRequestMessage request)
        {
            if (request == null)
            {
                throw new Exception("request");
            }

            //获取没有使用IOC注入的控制器(不在webconfig配置也可以)
            if (!BlackList.Contains(base.ControllerName))
            {
                //结合Unity容器创建,注意Unity容器名称规定为"{控制器名称}+api"形式
                IUnityContainer unityContainer = new IocHelper(base.ControllerName + "Api").GetUnityContainer();
                if (unityContainer != null)
                {
                    IHttpController controller = (IHttpController)unityContainer.Resolve(this.ControllerType);
                    return(controller);
                }
            }

            //没有使用的IOC注入的控制器,使用框架提供的方式返回
            IHttpControllerActivator httpControllerActivator = this.Configuration.Services.GetHttpControllerActivator();

            return(httpControllerActivator.Create(request, this, this.ControllerType));
        }