示例#1
0
 // This method gets called by the runtime. Use this method to add services to the container.
 public void ConfigureServices(IServiceCollection services)
 {
     services.AddSwaggerGen(config =>
     {
         config.SwaggerDoc("v1", new OpenApiInfo {
             Title = "HaotianCloud Api", Version = "v1"
         });
         var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
         var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
         config.IncludeXmlComments(xmlPath, true); //添加控制器层注释(true表示显示控制器注释)
     });
     //缓存选择
     if (Configuration.GetSection("SystemConfig:CacheProvider").Value != Define.CACHEPROVIDER_REDIS)
     {
         services.AddMemoryCache();
     }
     else
     {
         //redis 注入服务
         string redisConnectiong = Configuration.GetSection("SystemConfig:RedisConnectionString").Value;
         // 多客户端 1、基础 2、操作日志
         var redisDB1 = new CSRedisClient(redisConnectiong + ",defaultDatabase=" + 0);
         BaseHelper.Initialization(redisDB1);
         var redisDB2 = new CSRedisClient(redisConnectiong + ",defaultDatabase=" + 1);
         HandleLogHelper.Initialization(redisDB2);
         services.AddSingleton(redisDB1);
         services.AddSingleton(redisDB2);
     }
     //注入数据库连接
     services.AddScoped <Chloe.IDbContext>((serviceProvider) =>
     {
         return(DBContexHelper.Contex());
     });
     //代替HttpContext.Current
     services.AddHttpContextAccessor();
     services.AddHttpClient();
     services.AddOptions();
     //跨域
     services.AddCors();
     services.AddControllers(options =>
     {
         options.Filters.Add <ModelActionFilter>();
         options.ModelMetadataDetailsProviders.Add(new ModelBindingMetadataProvider());
     }).AddNewtonsoftJson(options =>
     {
         options.SerializerSettings.ContractResolver = new DefaultContractResolver();
     }).ConfigureApiBehaviorOptions(options =>
     {
         options.SuppressModelStateInvalidFilter = true;
     });
     services.AddControllers().AddControllersAsServices();
     services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(GlobalContext.HostingEnvironment.ContentRootPath + Path.DirectorySeparatorChar + "DataProtection"));
     GlobalContext.SystemConfig  = Configuration.GetSection("SystemConfig").Get <SystemConfig>();
     GlobalContext.Services      = services;
     GlobalContext.Configuration = Configuration;
 }
示例#2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            services.AddSession();
            //代替HttpContext.Current
            services.AddHttpContextAccessor();
            //缓存缓存选择
            if (Configuration.GetSection("SystemConfig:CacheProvider").Value != Define.CACHEPROVIDER_REDIS)
            {
                services.AddMemoryCache();
            }
            else
            {
                //redis 注入服务
                string redisConnectiong = Configuration.GetSection("SystemConfig:RedisConnectionString").Value;
                // 多客户端 1、基础 2、操作日志
                var redisDB1 = new CSRedisClient(redisConnectiong + ",defaultDatabase=" + 0);
                BaseHelper.Initialization(redisDB1);
                var redisDB2 = new CSRedisClient(redisConnectiong + ",defaultDatabase=" + 1);
                HandleLogHelper.Initialization(redisDB2);
                services.AddSingleton(redisDB1);
                services.AddSingleton(redisDB2);
            }
            #region 依赖注入
            //注入数据库连接
            services.AddScoped <Chloe.IDbContext>((serviceProvider) =>
            {
                return(DBContexHelper.Contex());
            });
            #region 注入 Quartz调度类
            services.AddSingleton <JobCenter>();
            services.AddSingleton <JobExecute>();
            //注册ISchedulerFactory的实例。
            services.AddSingleton <ISchedulerFactory, StdSchedulerFactory>();
            services.AddSingleton <IJobFactory, IOCJobFactory>();
            #endregion
            //注入SignalR实时通讯,默认用json传输
            services.AddSignalR(options =>
            {
                //客户端发保持连接请求到服务端最长间隔,默认30秒,改成4分钟,网页需跟着设置connection.keepAliveIntervalInMilliseconds = 12e4;即2分钟
                options.ClientTimeoutInterval = TimeSpan.FromMinutes(4);
                //服务端发保持连接请求到客户端间隔,默认15秒,改成2分钟,网页需跟着设置connection.serverTimeoutInMilliseconds = 24e4;即4分钟
                options.KeepAliveInterval = TimeSpan.FromMinutes(2);
            });
            ////注册html解析
            //services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));
            ////注册特性
            //services.AddScoped<HandlerLoginAttribute>();
            //services.AddScoped<HandlerAuthorizeAttribute>();
            ////ajax不能使用注入
            ////services.AddScoped<HandlerAjaxOnlyAttribute>();
            //services.AddScoped<HandlerAdminAttribute>();
            //////定时任务(已废除)
            ////services.AddBackgroundServices();
            #endregion
            services.AddHttpClient();

            services.AddControllersWithViews(options =>
            {
                options.Filters.Add <GlobalExceptionFilter>();
                options.Filters.Add <ModelActionFilter>();
                options.ModelMetadataDetailsProviders.Add(new ModelBindingMetadataProvider());
                options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
            }).AddNewtonsoftJson(options =>
            {
                // 返回数据首字母不小写,CamelCasePropertyNamesContractResolver是小写
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
            });
            services.AddAntiforgery(options => options.HeaderName = "X-CSRF-TOKEN");
            services.AddControllersWithViews().AddControllersAsServices();
            //调试前端可更新
            services.AddControllersWithViews().AddRazorRuntimeCompilation();
            services.AddOptions();
            services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(GlobalContext.HostingEnvironment.ContentRootPath + Path.DirectorySeparatorChar + "DataProtection"));
            GlobalContext.SystemConfig  = Configuration.GetSection("SystemConfig").Get <SystemConfig>();
            GlobalContext.Services      = services;
            GlobalContext.Configuration = Configuration;
            //更新数据库管理员和主系统
            try
            {
                var context     = DBContexHelper.Contex();
                var _setService = new Service.SystemOrganize.SystemSetService(context);
                Domain.SystemOrganize.SystemSetEntity temp = new Domain.SystemOrganize.SystemSetEntity();
                temp.F_AdminAccount  = GlobalContext.SystemConfig.SysemUserCode;
                temp.F_AdminPassword = GlobalContext.SystemConfig.SysemUserPwd;
                temp.F_DBProvider    = GlobalContext.SystemConfig.DBProvider;
                temp.F_DbString      = GlobalContext.SystemConfig.DBConnectionString;
                _setService.SubmitForm(temp, GlobalContext.SystemConfig.SysemMasterProject);
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex);
            }
        }