예제 #1
0
파일: Manage.cs 프로젝트: wangzhkai/sunmvc
        /// <summary>
        /// 初始化。
        /// </summary>
        public void Initialize()
        {
            //注册插件控制器工厂。
            ControllerBuilder.Current.SetControllerFactory(new PluginControllerFactory());
            //注册插件模板引擎。
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new PluginRazorViewEngine());

            var         plugins         = fileParser.Load();
            List <Type> controllerTypes = new List <Type>();

            foreach (var plugin in plugins)
            {
                //初始化插件。
                if (plugin.Status == PluginStatus.Usable)
                {
                    InitPlugin(plugin);
                }
                if (plugin.Status == PluginStatus.Usable && plugin.Type == PluginType.Module)
                {
                    var baseType = typeof(AreaRegistration);
                    foreach (var t in plugin.Types)
                    {
                        if (t.BaseType.Name == baseType.Name)
                        {
                            AreaRegistration registration = (AreaRegistration)Activator.CreateInstance(t);
                            registration.CreateContextAndRegister(RouteTable.Routes, null);
                        }
                        else if (t != null &&
                                 t.IsPublic &&
                                 t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) &&
                                 !t.IsAbstract &&
                                 typeof(IController).IsAssignableFrom(t))
                        {
                            controllerTypes.Add(t);
                        }
                    }
                }
            }
            RouteTable.Routes.MapMvcAttributeRoutesForTypes(controllerTypes);
            RouteManage.RegisterAllRoutes(RouteTable.Routes);
            BundleManage.RegisterAllBundles(BundleTable.Bundles);
            WebIoc.Register(b => b.RegisterTypeFromDirectory(null, AppDomain.CurrentDomain.SetupInformation.PrivateBinPath));
            WebIoc.Register(b => b.RegisterTypeFromDirectory(null, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App_Data\Plugins")));
            WebIoc.Register(b => b.RegisterControllersFromDirectory(null, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App_Data\Plugins")));
            WebIoc.Register(b => b.RegisterControllers(AppDomain.CurrentDomain.GetAssemblies()));
            WebIoc.Instance.OnBuilded += c => DependencyResolver.SetResolver(new AutofacDependencyResolver(c));
            WebIoc.Instance.Build();
        }