Пример #1
0
        public object ExecuteAsync(string controllerName, string actionName, IOwinContext context)
        {
            var controllerType = ApplicationAssemblyLoader.FindControllerTypeByName(controllerName);

            if (controllerType == null)
            {
                throw new NullReferenceException("Not Found Controller Name by" + controllerName);
            }

            var controller = (Controller)Activator.CreateInstance(controllerType);
            var pi         = controllerType.GetTypeInfo().GetProperty("Context", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);

            pi.SetValue(controller, context);
            var actionMethodInfo = controllerType.GetTypeInfo().GetMethod(actionName);

            if (actionMethodInfo == null)
            {
                throw new NullReferenceException("Not Found Action Name by" + actionName);
            }

            IDynamicMethodInvoker invoker = null;

            if (!actionInvokerCache.TryGetValue(actionMethodInfo, out invoker))
            {
                invoker = new DynamicMethodInvoker(actionMethodInfo);
                actionInvokerCache.TryAdd(actionMethodInfo, invoker);
            }

            var parameterInfoList = actionMethodInfo.GetParameters();

            var actionParams = parameterInfoList.ToActionParameters();

            var paramValues = ActionRuntimeParameter.GetValuesByRequest(actionParams, context.Request);

            var    filter = actionMethodInfo.GetCustomAttribute(typeof(ActionFilterAttribute), true) as ActionFilterAttribute;
            object result = null;

            if (filter != null)
            {
                ActionExecuteContext executeContext = new ActionExecuteContext(context, controllerName, actionName, invoker, parameterInfoList);

                if (filter.OnActionExecuting(executeContext))
                {
                    executeContext.Result = invoker.Invoke(controller, paramValues.ToArray());
                }

                filter.OnActionExecuted(executeContext);
                result = executeContext.Result;
            }
            else
            {
                result = invoker.Invoke(controller, paramValues.ToArray());
            }

            return(result);
        }
Пример #2
0
        public static void LoadViewEngine()
        {
            var viewEngineTypes = ApplicationAssemblyLoader.TypesOf(typeof(IViewEngine));

            foreach (var viewEngineType in viewEngineTypes)
            {
                var viewEngine = (IViewEngine)Activator.CreateInstance(viewEngineType);
                viewEngineCache.Add(viewEngine.ExtensionName, viewEngine);
            }
        }
Пример #3
0
        public void LoadRuntimeFileSystem(string path)
        {
            var providerTypes = ApplicationAssemblyLoader.TypesOf(typeof(IActionRuntimeProvider));

            foreach (var ptype in providerTypes)
            {
                RuntimeProviders.Add((IActionRuntimeProvider)Activator.CreateInstance(ptype));
            }

            foreach (var runtime in RuntimeProviders)
            {
                runtime.LoadRuntime(path);
                foreach (string controllerName in runtime.GetControllerNames())
                {
                    ca.Add(controllerName, runtime);
                }
            }
        }
Пример #4
0
        public static void AddYOYOFx(this IServiceCollection services)
        {
            services.AddSingleton <IControllerFacotry, DefaultControllerFactory>();
            services.AddSingleton <IRouteBuilder>(RouteBuilder.Builder);

            IYOYOBootstrapper bootStrapper = new DefaultBootstrapper();

            bootStrapper.Initialise();

            services.AddSingleton <IYOYOBootstrapper>(bootStrapper);

            services.Scan(scan => scan.FromAssemblies(ApplicationAssemblyLoader.GetLoadedAssemblies())
                          .AddClasses()
                          .UsingAttributes());

            services.AddSingleton <ISessionProvider, DefaultSessionProvider>();

            Application.CurrentApplication.ServiceProvider = services.BuildServiceProvider();
        }
        public void LoadRuntime(string path)
        {
            IRouteBuilder routeBuilder = RouteBuilder.Builder;

            ApplicationAssemblyLoader.GetControllerNames();
        }
 public string[] GetControllerNames()
 {
     return(ApplicationAssemblyLoader.GetControllerNames());
 }
        public object ExecuteAsync(string controllerName, string actionName, IOwinContext context)
        {
            var serviceProvider = Application.CurrentApplication.ServiceProvider;

            var controllerType = ApplicationAssemblyLoader.FindControllerTypeByName(controllerName);

            if (controllerType == null)
            {
                throw new NullReferenceException("Not Found Controller Name by" + controllerName);
            }

            var controllerFactory = (IControllerFacotry)serviceProvider.
                                    GetService(typeof(IControllerFacotry));


            var controller = controllerFactory.CreateController(controllerType, serviceProvider);

            controller.SetHttpContext(context);

            var actionMethodInfo = controllerType.GetTypeInfo().GetMethod(actionName);

            if (actionMethodInfo == null)
            {
                throw new NullReferenceException("Not Found Action Name by" + actionName);
            }

            IDynamicMethodInvoker invoker = null;

            if (!actionInvokerCache.TryGetValue(actionMethodInfo, out invoker))
            {
                invoker = new DynamicMethodInvoker(actionMethodInfo);
                actionInvokerCache.TryAdd(actionMethodInfo, invoker);
            }

            var parameterInfoList = actionMethodInfo.GetParameters();

            var actionParams = parameterInfoList.ToActionParameters();

            var paramValues = ActionRuntimeParameter.GetValuesByRequest(actionParams, context.Request);

            var    filter = actionMethodInfo.GetCustomAttribute(typeof(ActionFilterAttribute), true) as ActionFilterAttribute;
            object result = null;

            if (filter != null)
            {
                ActionExecuteContext executeContext = new ActionExecuteContext(context, controllerName, actionName, invoker, parameterInfoList);

                if (filter.OnActionExecuting(executeContext))
                {
                    executeContext.Result = invoker.Invoke(controller, paramValues.ToArray());
                }

                filter.OnActionExecuted(executeContext);
                result = executeContext.Result;
            }
            else
            {
                result = invoker.Invoke(controller, paramValues.ToArray());
            }

            return(result);
        }