Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="invocation">方法调用信息</param>
        public void Intercept(IInvocation invocation)
        {
            //If method call is for generic type (T)...
            if (DynamicApiControllerActionHelper.IsMethodOfType(invocation.Method, typeof(T)))
            {
                //Call real object's method
                try
                {
                    invocation.ReturnValue = invocation.Method.Invoke(_proxiedObject, invocation.Arguments);
                }
                catch (TargetInvocationException targetInvocation)
                {
                    if (targetInvocation.InnerException != null)
                    {
                        targetInvocation.InnerException.ReThrow();
                    }

                    throw;
                }
            }
            else
            {
                //Call api controller's methods as usual.
                invocation.Proceed();
            }
        }
        /// <summary>
        /// Intercepts method calls of dynamic api controller
        /// </summary>
        /// <param name="invocation">Method invocation information</param>
        public void Intercept(IInvocation invocation)
        {
            //If method call is for generic type (T)...
            if (DynamicApiControllerActionHelper.IsMethodOfType(invocation.Method, typeof(T)))
            {
                //Call real object's method
                try
                {
                    // *King
                    //var dm = new DynamicMethod("test", invocation.Method.ReturnType, invocation.Method.GetParameters().Select(p=>p.ParameterType).ToAr-ray());
                    //var il = dm.GetILGenerator();
                    //il.EmitCall(OpCodes.Call, invocation.Method, null);
                    //dm.Invoke(_proxiedObject, invocation.Arguments);
                    invocation.ReturnValue = invocation.Method.Invoke(_proxiedObject, invocation.Arguments);
                }
                catch (TargetInvocationException targetInvocation)
                {
                    if (targetInvocation.InnerException != null)
                    {
                        targetInvocation.InnerException.ReThrow();
                    }

                    throw;
                }
            }
            else
            {
                //Call api controller's methods as usual.
                invocation.Proceed();
            }
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 5
0
        /// <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);
            }
        }
Ejemplo n.º 6
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;
            }
        }
 public void Intercept(IInvocation invocation)
 {
     if (DynamicApiControllerActionHelper.IsMethodOfType(invocation.Method, typeof(T)))
     {
         try
         {
             invocation.ReturnValue = invocation.Method.Invoke(_proxiedObject, invocation.Arguments);
         }catch (TargetInvocationException targetInvocation)
         {
             if (targetInvocation.InnerException != null)
             {
                 ExceptionDispatchInfo.Capture(targetInvocation.InnerException).Throw();
             }
             throw;
         }
     }
     else
     {
         // 正常调用api controller的方法
         invocation.Proceed();
     }
 }