Startup.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ServiceModel;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Http.Features;
  8. using Microsoft.AspNetCore.Rewrite;
  9. using Microsoft.AspNetCore.StaticFiles;
  10. using Microsoft.Extensions.Configuration;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Microsoft.Extensions.FileProviders;
  13. using Microsoft.Extensions.Hosting;
  14. using System.Text;
  15. using System.Linq;
  16. using Microsoft.AspNetCore.Mvc.Razor;
  17. namespace MySystem
  18. {
  19. public class Startup
  20. {
  21. public Startup(IConfiguration configuration)
  22. {
  23. Configuration = configuration;
  24. }
  25. public IConfiguration Configuration { get; }
  26. // This method gets called by the runtime. Use this method to add services to the container.
  27. public void ConfigureServices(IServiceCollection services)
  28. {
  29. services.AddControllersWithViews();
  30. services.AddRouting(options =>
  31. {
  32. options.LowercaseUrls = true;
  33. });
  34. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  35. // services.AddCors(option => option.AddPolicy("cors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().SetIsOriginAllowed(_ => true)));
  36. services.AddMvc(options =>
  37. {
  38. options.EnableEndpointRouting = false;
  39. options.Filters.Add(typeof(GlobalExceptionsFilter));
  40. });
  41. //配置模版视图路径
  42. services.Configure<RazorViewEngineOptions>(options =>
  43. {
  44. options.ViewLocationExpanders.Add(new TemplateViewLocationExpander());
  45. });
  46. services.AddSession(options =>
  47. {
  48. // 设置 Session 过期时间
  49. options.IdleTimeout = TimeSpan.FromHours(1);
  50. options.Cookie.HttpOnly = true;
  51. });
  52. services.Configure<FormOptions>(x =>
  53. {
  54. x.MultipartBodyLengthLimit = 50 * 1024 * 1024;//不到300M
  55. });
  56. //生成密钥
  57. MySystemLib.SystemPublicFuction.appcheck = "success";
  58. }
  59. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  60. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  61. {
  62. if (env.IsDevelopment())
  63. {
  64. app.UseDeveloperExceptionPage();
  65. Library.ConfigurationManager.EnvironmentFlag = 1;
  66. }
  67. else
  68. {
  69. app.UseExceptionHandler("/Home/Error");
  70. app.UseHsts();
  71. Library.ConfigurationManager.EnvironmentFlag = 2;
  72. }
  73. Library.function.WritePage("/", "WebRootPath.txt", env.WebRootPath);
  74. app.UseStaticFiles();
  75. app.UseStaticFiles(new StaticFileOptions
  76. {
  77. ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
  78. {
  79. { ".apk", "application/vnd.android.package-archive" }
  80. })
  81. });
  82. app.UseCors("cors");
  83. app.UseAuthentication();
  84. app.UseRouting();
  85. app.UseAuthorization();
  86. app.UseSession();
  87. app.UseEndpoints(endpoints =>
  88. {
  89. endpoints.MapControllerRoute(
  90. name: "default",
  91. pattern: "{controller=Home}/{action=Index}/{Id?}");
  92. });
  93. initMainServer();
  94. initBsServer();
  95. StartHelper.Instance.Start(); //开启线程
  96. }
  97. //初始化数据结构
  98. private void initMainServer()
  99. {
  100. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  101. string connstr = Configuration["Setting:SqlConnStr"];
  102. System.Data.DataTable tablecollection = Library.CustomerSqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'AdMainServer'", connstr);
  103. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  104. {
  105. Dictionary<string, string> Columns = new Dictionary<string, string>();
  106. System.Data.DataTable columncollection = Library.CustomerSqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'AdMainServer' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'", connstr);
  107. foreach (System.Data.DataRow column in columncollection.Rows)
  108. {
  109. string datatype = column["DATA_TYPE"].ToString();
  110. if (datatype == "decimal")
  111. {
  112. datatype = "numeric";
  113. }
  114. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  115. }
  116. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  117. }
  118. AppConfig.Base.mainTables = tables;
  119. }
  120. private void initBsServer()
  121. {
  122. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  123. string connstr = Configuration["Setting:BsSqlConnStr"];
  124. System.Data.DataTable tablecollection = Library.CustomerSqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'AdConfigServer'", connstr);
  125. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  126. {
  127. Dictionary<string, string> Columns = new Dictionary<string, string>();
  128. System.Data.DataTable columncollection = Library.CustomerSqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'AdConfigServer' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'", connstr);
  129. foreach (System.Data.DataRow column in columncollection.Rows)
  130. {
  131. string datatype = column["DATA_TYPE"].ToString();
  132. if (datatype == "decimal")
  133. {
  134. datatype = "numeric";
  135. }
  136. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  137. }
  138. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  139. }
  140. AppConfig.Base.dbTables = tables;
  141. }
  142. }
  143. }