Startup.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Bowin.Common.Cache;
  6. using Bowin.Common.DES;
  7. using Bowin.Common.Filters;
  8. using Bowin.Common.Scheduler;
  9. using Bowin.Common.ServiceToken;
  10. using Bowin.Common.Utility;
  11. using YLShipBuildLandMap.Entity;
  12. using YLShipBuildLandMap.Entity.ViewModel;
  13. using YLShipBuildLandMap.Services.SystemSetting;
  14. using Microsoft.AspNetCore.Builder;
  15. using Microsoft.AspNetCore.Hosting;
  16. using Microsoft.AspNetCore.StaticFiles;
  17. using Microsoft.EntityFrameworkCore;
  18. using Microsoft.Extensions.Caching.Memory;
  19. using Microsoft.Extensions.Configuration;
  20. using Microsoft.Extensions.DependencyInjection;
  21. using Microsoft.Extensions.Hosting;
  22. using Newtonsoft.Json.Serialization;
  23. using Microsoft.AspNetCore.Http;
  24. using Microsoft.EntityFrameworkCore.Infrastructure;
  25. using YLShipBuildLandMap.Entity.Extensions;
  26. using System.Reflection;
  27. using Bowin.Common.ServiceToken.ApiIdentity;
  28. namespace YLShipBuildLandMap.Web
  29. {
  30. public class Startup
  31. {
  32. public Startup(IConfiguration configuration)
  33. {
  34. Configuration = configuration;
  35. }
  36. public IConfiguration Configuration { get; }
  37. // This method gets called by the runtime. Use this method to add services to the container.
  38. public void ConfigureServices(IServiceCollection services)
  39. {
  40. ServiceConfiguration.RegistLocalServices(services);
  41. services.AddDbContext<YLShipBuildLandMapContext>(options =>
  42. options.UseSqlServer(Configuration.GetConnectionString("DataContext"), builder =>
  43. {
  44. /*builder.UseRowNumberForPaging();
  45. */
  46. })
  47. ).AddScoped<YLShipBuildLandMapContext>();
  48. services.AddServiceAssembly(Assembly.Load("YLShipBuildLandMap.Services"), Assembly.Load("YLShipBuildLandMap.IServices"));
  49. //services.BuildServiceProvider();
  50. services.UseCache<MemoryCacheProvider>();
  51. //配置缓存为3分钟
  52. services.AddOptions().Configure<MemoryCacheEntryOptions>(Configuration.GetSection(nameof(MemoryCacheEntryOptions)))
  53. .Configure<MemoryCacheEntryOptions>(op =>
  54. {
  55. //op.SlidingExpiration = TimeSpan.FromMinutes(3);
  56. op.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1);
  57. });
  58. services.AddBowinAuthentication<LoginUser>(x => HttpHelper.GetService<IUserService>().GetLoginUserById(x),
  59. x => HttpHelper.GetService<IFunctionCodeService>().GetUserFunctionCodes(x));
  60. services.AddQuartz();
  61. services.AddControllers(configure => {
  62. configure.Filters.Add(typeof(CustomerExceptionFilter));
  63. })
  64. .AddNewtonsoftJson(options => {
  65. options.SerializerSettings.ContractResolver = new DefaultContractResolver();
  66. options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
  67. options.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
  68. });
  69. //services.AddSingleton<IDesAccessor, DesAccessor>();
  70. services.UseBowinDes();
  71. services.AddApiIdentity<YLShipBuildLandMap.Services.SystemSetting.ISystemService>();
  72. //允许一个或多个来源可以跨域
  73. /*services.AddCors(options =>
  74. {
  75. options.AddPolicy("CustomCorsPolicy", policy =>
  76. {
  77. // 设定允许跨域的来源,有多个可以用','隔开
  78. policy.SetIsOriginAllowed(x => true) // .WithOrigins(YLShipBuildLandMap.Web.Configuration.Current.CROSDomainList.ToArray())
  79. .AllowAnyHeader()
  80. .AllowAnyMethod()
  81. .AllowCredentials();
  82. });
  83. });*/
  84. services.AddHttpClient();
  85. }
  86. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  87. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  88. {
  89. if (env.IsDevelopment())
  90. {
  91. app.UseDeveloperExceptionPage();
  92. }
  93. else
  94. {
  95. app.UseExceptionHandler("/Error");
  96. }
  97. app.UseDefaultFiles(new DefaultFilesOptions
  98. {
  99. DefaultFileNames = new List<string>
  100. {
  101. "default.html"
  102. }
  103. });
  104. var provider = new FileExtensionContentTypeProvider();
  105. provider.Mappings[".properties"] = "application/octet-stream";
  106. app.UseStaticFiles(new StaticFileOptions
  107. {
  108. ContentTypeProvider = provider
  109. });
  110. YLShipBuildLandMapContext.SetServices(app.ApplicationServices);
  111. app.UseWebSockets();
  112. app.UseRouting();
  113. app.UseAuthentication();
  114. app.UseAuthorization();
  115. //配置Cors
  116. //设定特定ip允许跨域 CustomCorsPolicy是在ConfigureServices方法中配置的跨域策略名称
  117. //app.UseCors("CustomCorsPolicy");
  118. app.StartSchedulerJobs();
  119. //app.Use(next=> context => {
  120. // context.Request.EnableBuffering();
  121. // return next(context);
  122. // });
  123. app.UseEndpoints(endpoints =>
  124. {
  125. endpoints.MapControllers();
  126. //endpoints.MapHub<TokenSender>("/hubs/token");
  127. });
  128. }
  129. }
  130. }