Exemplo n.º 1
0
        /// <summary>
        /// Configure
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        /// <param name="db"></param>
        /// <param name="hangfire"></param>
        /// <param name="luceneIndexerOptions"></param>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DataContext db, IHangfireBackJob hangfire, LuceneIndexerOptions luceneIndexerOptions)
        {
            ServiceProvider = app.ApplicationServices;
            app.UseForwardedHeaders().UseCertificateForwarding(); // X-Forwarded-For
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/ServiceUnavailable");
            }

            db.Database.EnsureCreated();
            var dic = db.SystemSetting.ToDictionary(s => s.Name, s => s.Value); //初始化系统设置参数

            foreach (var(key, value) in dic)
            {
                CommonHelper.SystemSettings.TryAdd(key, value);
            }
            app.UseBundles();
            UseLuceneSearch(env, hangfire, luceneIndexerOptions);
            if (bool.Parse(Configuration["Https:Enabled"]))
            {
                app.UseHttpsRedirection().UseRewriter(new RewriteOptions().AddRedirectToNonWww()); // URL重写
            }

            app.UseDefaultFiles().UseStaticFiles(new StaticFileOptions //静态资源缓存策略
            {
                OnPrepareResponse = context =>
                {
                    context.Context.Response.Headers[HeaderNames.CacheControl] = "public,no-cache";
                    context.Context.Response.Headers[HeaderNames.Expires]      = DateTime.UtcNow.AddDays(7).ToString("R");
                },
                ContentTypeProvider = new FileExtensionContentTypeProvider(MimeMapper.MimeTypes),
            });
            app.UseSession().UseCookiePolicy().UseMiniProfiler(); //注入Session
            app.UseRequestIntercept();                            //启用网站请求拦截
            app.UseStaticHttpContext();                           //注入静态HttpContext对象

            app.UseHangfireServer().UseHangfireDashboard("/taskcenter", new DashboardOptions()
            {
                Authorization = new[]
                {
                    new MyRestrictiveAuthorizationFilter()
                }
            });                                                //配置hangfire
            //app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()); //配置跨域
            app.UseResponseCaching().UseResponseCompression(); //启动Response缓存
            app.UseRouting();                                  // 放在 UseStaticFiles 之后
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();                                                        // 属性路由
                endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"); // 默认路由
                endpoints.MapHub <MyHub>("/hubs");
            });
            HangfireJobInit.Start(); //初始化定时任务
            Console.WriteLine("网站启动完成");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Configure
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        /// <param name="db"></param>
        /// <param name="hangfire"></param>
        /// <param name="luceneIndexerOptions"></param>
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, DataContext db, IHangfireBackJob hangfire, LuceneIndexerOptions luceneIndexerOptions)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseException();
            }

            //db.Database.Migrate();

            ConfigureLuceneSearch(env, hangfire, luceneIndexerOptions);

            app.UseResponseCompression();
            if (bool.Parse(Configuration["Https:Enabled"]))
            {
                app.UseHttpsRedirection().UseRewriter(new RewriteOptions().AddRedirectToNonWww()); // URL重写
            }

            app.UseStaticHttpContext();              //注入静态HttpContext对象
            app.UseSession().UseCookiePolicy();      //注入Session

            app.UseStaticFiles(new StaticFileOptions //静态资源缓存策略
            {
                OnPrepareResponse = context =>
                {
                    context.Context.Response.Headers[HeaderNames.CacheControl] = "public,no-cache";
                    context.Context.Response.Headers[HeaderNames.Expires]      = DateTime.UtcNow.AddDays(7).ToString("R");
                },
                ContentTypeProvider = new FileExtensionContentTypeProvider(MimeMapper.MimeTypes),
            });

            app.UseRequestIntercept();                                          //启用网站防火墙
            var dic = db.SystemSetting.ToDictionary(s => s.Name, s => s.Value); //初始化系统设置参数

            foreach (var(key, value) in dic)
            {
                CommonHelper.SystemSettings.TryAdd(key, value);
            }

            app.UseHangfireServer().UseHangfireDashboard("/taskcenter", new DashboardOptions()
            {
                Authorization = new[]
                {
                    new MyRestrictiveAuthorizationFilter()
                }
            });                                                                                                    //配置hangfire
            app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials()); //配置跨域
            app.UseResponseCaching().UseResponseCompression();                                                     //启动Response缓存
            app.UseSignalR(hub => hub.MapHub <MyHub>("/hubs"));
            HangfireJobInit.Start();                                                                               //初始化定时任务
            app.UseMvcWithDefaultRoute();
        }
Exemplo n.º 3
0
 public static void SetupHangfire(this IApplicationBuilder app)
 {
     app.UseHangfireDashboard("/taskcenter", new DashboardOptions()
     {
         Authorization = new[]
         {
             new MyRestrictiveAuthorizationFilter()
         }
     });                      //配置hangfire
     HangfireJobInit.Start(); //初始化定时任务
 }
Exemplo n.º 4
0
        /// <summary>
        /// Configure
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        /// <param name="db"></param>
        /// <param name="hangfire"></param>
        /// <param name="luceneIndexerOptions"></param>
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, DataContext db, IHangfireBackJob hangfire, LuceneIndexerOptions luceneIndexerOptions)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                //app.UseHsts();
                app.UseException();
            }

            //db.Database.Migrate();

            #region 导词库

            Console.WriteLine("正在导入自定义词库...");
            double time = HiPerfTimer.Execute(() =>
            {
                var lines     = File.ReadAllLines(Path.Combine(env.ContentRootPath, "App_Data", "CustomKeywords.txt"));
                var segmenter = new JiebaSegmenter();
                foreach (var word in lines)
                {
                    segmenter.AddWord(word);
                }
            });
            Console.WriteLine($"导入自定义词库完成,耗时{time}s");

            #endregion

            string lucenePath = Path.Combine(env.ContentRootPath, luceneIndexerOptions.Path);
            if (!Directory.Exists(lucenePath) || Directory.GetFiles(lucenePath).Length < 1)
            {
                Console.WriteLine(",索引库不存在,开始自动创建Lucene索引库...");
                hangfire.CreateLuceneIndex();
                Console.WriteLine("索引库创建完成!");
            }

            app.UseResponseCompression();
            app.UseHttpsRedirection().UseRewriter(new RewriteOptions().AddRedirectToNonWww()); // URL重写
            app.UseStaticHttpContext();                                                        //注入静态HttpContext对象

            app.UseSession().UseCookiePolicy();                                                //注入Session

            app.UseStaticFiles(new StaticFileOptions                                           //静态资源缓存策略
            {
                OnPrepareResponse = context =>
                {
                    context.Context.Response.Headers[HeaderNames.CacheControl] = "public,no-cache";
                    context.Context.Response.Headers[HeaderNames.Expires]      = DateTime.UtcNow.AddDays(7).ToString("R");
                },
                ContentTypeProvider = new FileExtensionContentTypeProvider(MimeMapper.MimeTypes),
            });

            app.UseFirewall().UseRequestIntercept();                                                //启用网站防火墙
            CommonHelper.SystemSettings = db.SystemSetting.ToDictionary(s => s.Name, s => s.Value); //初始化系统设置参数

            app.UseHangfireServer().UseHangfireDashboard("/taskcenter", new DashboardOptions()
            {
                Authorization = new[]
                {
                    new MyRestrictiveAuthorizationFilter()
                }
            }); //配置hangfire
            app.UseCors(builder =>
            {
                builder.AllowAnyHeader();
                builder.AllowAnyMethod();
                builder.AllowAnyOrigin();
                builder.AllowCredentials();
            });                       //配置跨域
            app.UseResponseCaching(); //启动Response缓存
            app.UseSignalR(hub => hub.MapHub <MyHub>("/hubs"));
            HangfireJobInit.Start();  //初始化定时任务
            app.UseMvcWithDefaultRoute();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Configure
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        /// <param name="db"></param>
        /// <param name="hangfire"></param>
        /// <param name="luceneIndexerOptions"></param>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DataContext db, IHangfireBackJob hangfire, LuceneIndexerOptions luceneIndexerOptions)
        {
            ServiceProvider = app.ApplicationServices;
            db.Database.EnsureCreated();
            InitSettings(db);
            UseLuceneSearch(env, hangfire, luceneIndexerOptions);

            app.UseForwardedHeaders().UseCertificateForwarding(); // X-Forwarded-For
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/ServiceUnavailable");
            }

            app.UseBundles();
            if (bool.Parse(Configuration["Https:Enabled"]))
            {
                app.UseHttpsRedirection();
            }

            switch (Configuration["UseRewriter"])
            {
            case "NonWww":
                app.UseRewriter(new RewriteOptions().AddRedirectToNonWww(301));     // URL重写
                break;

            case "WWW":
                app.UseRewriter(new RewriteOptions().AddRedirectToWww(301));     // URL重写
                break;
            }

            app.UseDefaultFiles().UseStaticFiles(new StaticFileOptions //静态资源缓存策略
            {
                OnPrepareResponse = context =>
                {
                    context.Context.Response.Headers[HeaderNames.CacheControl] = "public,no-cache";
                    context.Context.Response.Headers[HeaderNames.Expires]      = DateTime.Now.AddDays(7).ToString("R");
                },
                ContentTypeProvider = new FileExtensionContentTypeProvider(MimeMapper.MimeTypes),
            });
            app.UseSession().UseCookiePolicy().UseMiniProfiler(); //注入Session
            app.UseRequestIntercept();                            //启用网站请求拦截

            app.UseHangfireServer().UseHangfireDashboard("/taskcenter", new DashboardOptions()
            {
                Authorization = new[]
                {
                    new MyRestrictiveAuthorizationFilter()
                }
            });                                                //配置hangfire
            app.UseResponseCaching().UseResponseCompression(); //启动Response缓存
            app.UseActivity();                                 // 抽奖活动
            app.UseRouting();                                  // 放在 UseStaticFiles 之后
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();                                                        // 属性路由
                endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"); // 默认路由
                endpoints.MapHub <MyHub>("/hubs");
            });

            HangfireJobInit.Start(); //初始化定时任务
            Console.WriteLine("网站启动完成");
        }