private static void UseLuceneSearch(IHostEnvironment env, IHangfireBackJob hangfire, LuceneIndexerOptions luceneIndexerOptions) { Task.Run(() => { 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"); }); string lucenePath = Path.Combine(env.ContentRootPath, luceneIndexerOptions.Path); if (!Directory.Exists(lucenePath) || Directory.GetFiles(lucenePath).Length < 1) { Console.WriteLine("索引库不存在,开始自动创建Lucene索引库..."); hangfire.CreateLuceneIndex(); Console.WriteLine("索引库创建完成!"); } }
private static void UseLuceneSearch(IHostEnvironment env, IHangfireBackJob hangfire, LuceneIndexerOptions luceneIndexerOptions) { Task.Run(() => { Console.WriteLine("正在导入自定义词库..."); double time = HiPerfTimer.Execute(() => { var set = ServiceProvider.GetRequiredService <DataContext>().Post.Select(p => $"{p.Title},{p.Label},{p.Keyword}").AsEnumerable().SelectMany(s => s.Split(new[] { ',', ' ', '+', '—' }, StringSplitOptions.RemoveEmptyEntries)).ToHashSet(); var lines = File.ReadAllLines(Path.Combine(env.ContentRootPath, "App_Data", "CustomKeywords.txt")).Union(set); var segmenter = new JiebaSegmenter(); foreach (var word in lines) { segmenter.AddWord(word); } }); Console.WriteLine($"导入自定义词库完成,耗时{time}s"); }); string lucenePath = Path.Combine(env.ContentRootPath, luceneIndexerOptions.Path); if (!Directory.Exists(lucenePath) || Directory.GetFiles(lucenePath).Length < 1) { Console.WriteLine("索引库不存在,开始自动创建Lucene索引库..."); hangfire.CreateLuceneIndex(); Console.WriteLine("索引库创建完成!"); } }
/// <summary> /// Configure /// </summary> /// <param name="app"></param> /// <param name="env"></param> /// <param name="hangfire"></param> /// <param name="luceneIndexerOptions"></param> public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHangfireBackJob hangfire, LuceneIndexerOptions luceneIndexerOptions) { ServiceProvider = app.ApplicationServices; var maindb = ServiceProvider.GetRequiredService <DataContext>(); var loggerdb = ServiceProvider.GetRequiredService <LoggerDbContext>(); maindb.Database.EnsureCreated(); if (loggerdb.Database.EnsureCreated()) { loggerdb.ApplyHypertables(); } app.InitSettings(); app.UseLuceneSearch(env, hangfire, luceneIndexerOptions); app.UseForwardedHeaders().UseCertificateForwarding(); // X-Forwarded-For if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/ServiceUnavailable"); } app.UseBundles(); app.SetupHttpsRedirection(Configuration); app.UseDefaultFiles().UseStaticFiles(); app.UseSession().UseCookiePolicy(); //注入Session app.UseWhen(c => c.Session.Get <UserInfoDto>(SessionKey.UserInfo)?.IsAdmin == true, builder => { builder.UseMiniProfiler(); builder.UseCLRStatsDashboard(); }); app.UseWhen(c => !c.Request.Path.StartsWithSegments("/_blazor"), builder => builder.UseMiddleware <RequestInterceptMiddleware>()); //启用网站请求拦截 app.SetupHangfire(); app.UseResponseCaching().UseResponseCompression(); //启动Response缓存 app.UseMiddleware <TranslateMiddleware>(); app.UseRouting().UseEndpoints(endpoints => { endpoints.MapBlazorHub(options => { options.ApplicationMaxBufferSize = 4194304; options.LongPolling.PollTimeout = TimeSpan.FromSeconds(10); options.TransportMaxBufferSize = 8388608; }); endpoints.MapControllers(); // 属性路由 endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"); // 默认路由 endpoints.MapFallbackToController("Index", "Error"); }); Console.WriteLine("网站启动完成"); }
/// <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.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("网站启动完成"); }
internal static void UseLuceneSearch(this IApplicationBuilder app, IHostEnvironment env, IHangfireBackJob hangfire, LuceneIndexerOptions luceneIndexerOptions) { var are = new AutoResetEvent(false); Task.Run(() => { Console.WriteLine("正在导入自定义词库..."); double time = HiPerfTimer.Execute(() => { var db = app.ApplicationServices.GetRequiredService <DataContext>(); using var set = db.Post.Select(p => $"{p.Title},{p.Label},{p.Keyword}").AsParallel().SelectMany(s => Regex.Split(s, @"\p{P}(?<!\.|#)|\p{Z}|\p{S}")).Where(s => s.Length > 1).ToPooledSet(); var lines = File.ReadAllLines(Path.Combine(env.ContentRootPath, "App_Data", "CustomKeywords.txt")).Union(set); KeywordsManager.AddWords(lines); KeywordsManager.AddSynonyms(File.ReadAllLines(Path.Combine(env.ContentRootPath, "App_Data", "CustomSynonym.txt")).Where(s => s.Contains(" ")).Select(s => { var arr = Regex.Split(s, "\\s"); return(arr[0], arr[1]); })); }); Console.WriteLine($"导入自定义词库完成,耗时{time}s"); Windows.ClearMemorySilent(); are.Set(); }); string lucenePath = Path.Combine(env.ContentRootPath, luceneIndexerOptions.Path); if (!Directory.Exists(lucenePath) || Directory.GetFiles(lucenePath).Length < 1) { are.WaitOne(); Console.WriteLine("索引库不存在,开始自动创建Lucene索引库..."); hangfire.CreateLuceneIndex(); Console.WriteLine("索引库创建完成!"); } }
internal static void UseLuceneSearch(this IApplicationBuilder app, IHostEnvironment env, IHangfireBackJob hangfire, LuceneIndexerOptions luceneIndexerOptions) { Task.Run(() => { Console.WriteLine("正在导入自定义词库..."); double time = HiPerfTimer.Execute(() => { var posts = app.ApplicationServices.GetRequiredService <DataContext>().Post; var set = posts.Select(p => p.Title).AsEnumerable().SelectMany(s => s.Split(',', ',', ' ', '+', '—', '(', ')', ':', '&', '(', ')', '-', '_', '[', ']')).Where(s => s.Length > 1).Union(posts.Select(p => $"{p.Label},{p.Keyword}").AsEnumerable().SelectMany(s => s.Split(','))).ToHashSet(); var lines = File.ReadAllLines(Path.Combine(env.ContentRootPath, "App_Data", "CustomKeywords.txt")).Union(set); var segmenter = new JiebaSegmenter(); foreach (var word in lines) { segmenter.AddWord(word); } }); Console.WriteLine($"导入自定义词库完成,耗时{time}s"); Windows.ClearMemorySilent(); }); string lucenePath = Path.Combine(env.ContentRootPath, luceneIndexerOptions.Path); if (!Directory.Exists(lucenePath) || Directory.GetFiles(lucenePath).Length < 1) { Console.WriteLine("索引库不存在,开始自动创建Lucene索引库..."); hangfire.CreateLuceneIndex(); Console.WriteLine("索引库创建完成!"); } }
/// <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(); }
/// <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"); } //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(); }