Beispiel #1
0
        static void Main(string[] args)
        {
            //1.对WebApi服务的替换
            ApiGlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), new AbpHttpControllerSelector(ApiGlobalConfiguration.Configuration));
            ApiGlobalConfiguration.Configuration.Services.Replace(typeof(IHttpActionSelector), new AbpApiControllerActionSelector());
            ApiGlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new AbpControllerActivator());
            //2.路由
            ApiGlobalConfiguration.Configuration.Routes.MapHttpRoute(
                "DynamicWebApi",
                "apiservice/{service}/{action}/{id}",
                new { id = RouteParameter.Optional }
                );
            //3.缓存Service
            var controllerInfo = new DynamicApiControllerInfo("Order", typeof(DynamicApiController <IOrderService>));

            foreach (var methodInfo in DynamicApiControllerActionHelper.GetMethodsOfType(typeof(IOrderService)))
            {
                controllerInfo.Actions[methodInfo.Name] = new DynamicApiActionInfo(methodInfo.Name, HttpMethod.Get, methodInfo);
            }
            DynamicApiControllerManager.Register(controllerInfo);
            //4.Owin
            const string url       = "http://localhost:8080/";
            var          startOpts = new StartOptions(url);

            using (WebApp.Start <Startup>(startOpts))
            {
                Console.WriteLine("Server run at " + url + " , press Enter to exit.");
                Console.ReadKey();
            }
        }
        public void Should_Find_Right_Methods()
        {
            var methods = DynamicApiControllerActionHelper.GetMethodsOfType(typeof(IMyApplicationService));

            methods.Count.ShouldBe(4);
            foreach (var method in methods)
            {
                DynamicApiControllerActionHelper.IsMethodOfType(method, typeof(IMyApplicationService)).ShouldBe(true);
            }
        }
        /// <summary>
        /// Creates a new instance of ApiControllerInfoBuilder.
        /// </summary>
        /// <param name="serviceName">Name of the controller</param>
        public ApiControllerBuilder(string serviceName)
        {
            if (string.IsNullOrWhiteSpace(serviceName))
            {
                throw new ArgumentException("serviceName null or empty!", "serviceName");
            }

            if (!DynamicApiServiceNameHelper.IsValidServiceName(serviceName))
            {
                throw new ArgumentException("serviceName is not properly formatted! It must contain a single-depth namespace at least! For example: 'myapplication/myservice'.", "serviceName");
            }

            _serviceName = serviceName;

            _actionBuilders = new Dictionary <string, ApiControllerActionBuilder <T> >();
            foreach (var methodInfo in DynamicApiControllerActionHelper.GetMethodsOfType(typeof(T)))
            {
                _actionBuilders[methodInfo.Name] = new ApiControllerActionBuilder <T>(this, methodInfo);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Creates a new instance of ApiControllerInfoBuilder.
        /// </summary>
        /// <param name="serviceName">Name of the controller</param>
        /// <param name="iocResolver">Ioc resolver</param>
        public DefaultControllerBuilder(string serviceName, IIocResolver iocResolver, IControllerRegister defaultControllerManager)
        {
            Check.NotNull(iocResolver, nameof(iocResolver));

            if (string.IsNullOrWhiteSpace(serviceName))
            {
                throw new ArgumentException("serviceName null or empty!", "serviceName");
            }

            if (!DynamicApiServiceNameHelper.IsValidServiceName(serviceName))
            {
                throw new ArgumentException("serviceName is not properly formatted! It must contain a single-depth namespace at least! For example: 'myapplication/myservice'.", "serviceName");
            }

            _iocResolver = iocResolver;
            _defaultControllerManager = defaultControllerManager;

            ServiceName          = serviceName;
            ServiceInterfaceType = typeof(T);

            _actionBuilders = new Dictionary <string, TControllerActionBuilder>();
            var methodInfos = DynamicApiControllerActionHelper.GetMethodsOfType(typeof(T))
                              .Where(methodInfo => methodInfo.GetSingleAttributeOrNull <BlocksActionNameAttribute>() != null);

            foreach (var methodInfo in methodInfos)
            {
                var actionBuilder     = (TControllerActionBuilder)typeof(TControllerActionBuilder).New(this, methodInfo, iocResolver);
                var remoteServiceAttr = methodInfo.GetSingleAttributeOrNull <RemoteServiceAttribute>();
                if (remoteServiceAttr != null && !remoteServiceAttr.IsEnabledFor(methodInfo))
                {
                    actionBuilder.DontCreateAction();
                }
                var actionNameAttr = methodInfo.GetSingleAttributeOrNull <BlocksActionNameAttribute>();


                _actionBuilders[actionNameAttr.ActionName] =
                    actionBuilder;
            }
        }