Пример #1
0
        /// <summary>
        /// 初始化程序安装步骤
        /// </summary>
        /// <returns></returns>
        void InitializeSetup()
        {
            var containerBuilder = new ContainerBuilder();

            containerBuilder.Register(c => new DefaultCacheService(new RuntimeMemoryCache(), 1.0F)).As <ICacheService>().SingleInstance();
            containerBuilder.RegisterAssemblyTypes(Assembly.Load("Spacebuilder.Common")).Where(t => t.Name.EndsWith("SettingsManager")).AsImplementedInterfaces().SingleInstance();
            containerBuilder.Register(c => new DefaultStoreProvider(@"~/Uploads")).As <IStoreProvider>().Named <IStoreProvider>("CommonStorageProvider").SingleInstance();
            //注册PageResourceManager
            bool pageResourceDebugEnabled = false;

            if (ConfigurationManager.AppSettings["PageResource:DebugEnabled"] != null)
            {
                if (!bool.TryParse(ConfigurationManager.AppSettings["PageResource:DebugEnabled"], out pageResourceDebugEnabled))
                {
                    pageResourceDebugEnabled = false;
                }
            }

            //注册标题图的配置
            TenantLogoSettings.RegisterSettings(LogoConfigManager.Instance().GetAllLogoConfigs());

            string resourceSite = null;

            if (ConfigurationManager.AppSettings["PageResource:Site"] != null)
            {
                resourceSite = ConfigurationManager.AppSettings["PageResource:Site"];
            }
            //用户业务逻辑
            containerBuilder.Register(c => new UserService()).As <IUserService>().SingleInstance();
            containerBuilder.Register(c => new PageResourceManager("Spacebuilder v4.3正式版")
            {
                ResourceSite = resourceSite
            }).As <IPageResourceManager>().InstancePerHttpRequest();
            IContainer container = containerBuilder.Build();

            //将Autofac容器中的实例注册到mvc自带DI容器中(这样才获取到每请求缓存的实例)
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            DIContainer.RegisterContainer(container);

            //注册皮肤选择器
            Type themeResolverType = Type.GetType("Spacebuilder.Setup.SetupThemeResolver,Spacebuilder.Setup");
            ConstructorInvoker themeResolverConstructor = themeResolverType.DelegateForCreateInstance();
            IThemeResolver     themeResolver            = themeResolverConstructor() as IThemeResolver;

            ThemeService.RegisterThemeResolver("Channel", themeResolver);

            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new ThemedViewEngine());

            string extensionForOldIIS = ".aspx";
            int    iisVersion         = 0;

            if (!int.TryParse(System.Configuration.ConfigurationManager.AppSettings["IISVersion"], out iisVersion))
            {
                iisVersion = 7;
            }
            if (iisVersion >= 7)
            {
                extensionForOldIIS = string.Empty;
            }

            RouteTable.Routes.MapRoute(
                "Channel_Home", // Route name
                string.IsNullOrEmpty(extensionForOldIIS) ? "" : "Home" + extensionForOldIIS,
                new { controller = "Install", action = "Start", area = "Setup" }
                );

            RouteTable.Routes.MapRoute(
                "Install_Home",  // Route name
                string.IsNullOrEmpty(extensionForOldIIS) ? "Install" : "Install" + extensionForOldIIS,
                new { controller = "Install", action = "Start", area = "Setup" }
                );

            RouteTable.Routes.MapRoute(
                "Install_Common",
                "Install/{action}" + extensionForOldIIS,
                new { controller = "Install", action = "Start", area = "Setup" });

            RouteTable.Routes.MapRoute(
                "Upgrade_Home",  // Route name
                "Upgrade" + extensionForOldIIS,
                new { controller = "Upgrade", action = "Ready", area = "Setup" }
                );

            RouteTable.Routes.MapRoute(
                "Upgrade_Common",
                "Upgrade/{action}" + extensionForOldIIS,
                new { controller = "Upgrade", action = "Ready", area = "Setup" });
        }
Пример #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            GlobalHostingEnvironment.HostingEnvironment = this.HostingEnvironment;
            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            // Add framework services.
            services.Replace(ServiceDescriptor.Transient <IControllerActivator, ServiceBasedControllerActivator>());
            services.AddAuthorization(authorization =>
            {
                authorization.AddPolicy("EnterSystem", policy => policy.Requirements.Add(new EnterSystemRequirement()));
            });
            services.AddSession(session =>
            {
                session.IdleTimeout = TimeSpan.FromMinutes(30);
            });
            services.AddTimedJob();
            services.AddMvc(option =>
            {
                option.Filters.Add(new HandleErrorToLogAttribute());
            }).AddJsonOptions(option => {
                option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm";
            });

            var assembly = this.GetType().GetTypeInfo().Assembly;
            var builder  = new ContainerBuilder();
            var manager  = new ApplicationPartManager();
            var feature  = new ControllerFeature();

            manager.ApplicationParts.Add(new AssemblyPart(assembly));
            manager.FeatureProviders.Add(new ControllerFeatureProvider());
            manager.PopulateFeature(feature);

            builder.RegisterTypes(feature.Controllers.Select(controller => controller.AsType()).ToArray()).PropertiesAutowired();
            builder.Register(c => new DbService(Configuration.GetSection("ConnectionStrings")["DefaultConnection"])).AsSelf().PropertiesAutowired();

            string[] assembliesName = { "SexyColor.BusinessComponents", "SexyColor.CommonComponents" };
            var      assemblies     = assembliesName.Select(w => Assembly.Load(new AssemblyName(w))).ToArray();


            //仓储批量注册
            builder.RegisterAssemblyTypes(assemblies).Where(t => t.Name.EndsWith("Repository")).AsSelf().AsImplementedInterfaces().SingleInstance().PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);

            //服务批量注册
            builder.RegisterAssemblyTypes(assemblies).Where(t => t.Name.EndsWith("Service")).AsSelf().AsImplementedInterfaces().SingleInstance().PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);

            //业务组件注册
            builder.Register(c => new AuthorizerHelper()).AsSelf().SingleInstance().PropertiesAutowired();
            builder.Register(c => new DefaultIdGenerator()).As <IdGenerator>().SingleInstance();
            builder.Register(c => new DefaultUserIdToUserNameDictionary()).As <UserIdToUserNameDictionary>().SingleInstance().PropertiesAutowired();
            builder.Register(c => new DefaultCaptchaManager()).As <ICaptchaManager>().SingleInstance();
            //builder.Register(c => new DefaultCacheService(new RedisMemoryCache(), new RuntimeMemoryCache(), 1.0F, true)).As<ICacheService>().SingleInstance();
            builder.Register(c => new DefaultCacheService(new RuntimeMemoryCache(), 1.0F)).As <ICacheService>().SingleInstance();
            builder.Register(c => new DefaultStorageService()).As <IStorageService>().SingleInstance();
            builder.Register(c => new DefaultStoreProvider(@"~/images")).As <IStoreProvider>().SingleInstance();
            builder.Register(c => new Logger(this.HostingEnvironment)).As <SexyColor.CommonComponents.ILogger>().SingleInstance();
            builder.Register(c => new Log4NetLoggerFactoryAdapter(repository.Name)).As <ILoggerFactoryAdapter>().SingleInstance();
            builder.Register(c => new QuartzTaskScheduler()).As <ITaskScheduler>().SingleInstance();
            //泛型注册
            builder.RegisterGeneric(typeof(Repository <>)).As(typeof(IRepository <>)).SingleInstance().PropertiesAutowired();

            builder.Populate(services);
            this.ApplicationContainer = builder.Build();
            BuilderContext.Register(services, Configuration);

            LogoSettings.RegisterSettings(LogoConfigManager.Instance().GetAllLogoConfigs());
            new DefaultResourceManager().Excute();
            ReadConfigurationHelper.Configuration = Configuration;
            DIContainer.RegisterContainer(this.ApplicationContainer);
            return(new AutofacServiceProvider(this.ApplicationContainer));
        }