Startup.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 OrderSystem.Entity;
  12. using OrderSystem.Entity.ViewModel;
  13. using OrderSystem.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 OrderSystem.Entity.Extensions;
  26. using System.Reflection;
  27. using Bowin.Common.ServiceToken.ApiIdentity;
  28. namespace OrderSystem.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<OrderSystemContext>(options =>
  42. options.UseSqlServer(Configuration.GetConnectionString("DataContext"), builder =>
  43. {
  44. /*builder.UseRowNumberForPaging();
  45. */
  46. })
  47. ).AddScoped<OrderSystemContext>();
  48. services.AddServiceAssembly(Assembly.Load("OrderSystem.Services"), Assembly.Load("OrderSystem.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>().GetUserById(x),
  59. null);
  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. //允许一个或多个来源可以跨域
  72. services.AddCors(options =>
  73. {
  74. options.AddPolicy("CustomCorsPolicy", policy =>
  75. {
  76. // 设定允许跨域的来源,有多个可以用','隔开
  77. policy.WithOrigins(OrderSystem.Web.Configuration.Current.CROSDomainList.ToArray())
  78. .AllowAnyHeader()
  79. .AllowAnyMethod()
  80. .AllowCredentials();
  81. });
  82. });
  83. services.AddHttpClient();
  84. }
  85. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  86. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  87. {
  88. if (env.IsDevelopment())
  89. {
  90. app.UseDeveloperExceptionPage();
  91. }
  92. else
  93. {
  94. app.UseExceptionHandler("/Error");
  95. }
  96. app.UseDefaultFiles(new DefaultFilesOptions
  97. {
  98. DefaultFileNames = new List<string>
  99. {
  100. "h5/"+ OrderSystem.Web.Configuration.Current.AppSettings.H5Version+"/index.html"
  101. }
  102. });
  103. var provider = new FileExtensionContentTypeProvider();
  104. provider.Mappings[".properties"] = "application/octet-stream";
  105. app.UseStaticFiles(new StaticFileOptions
  106. {
  107. ContentTypeProvider = provider
  108. });
  109. app.UseWebSockets();
  110. app.UseRouting();
  111. app.UseAuthentication();
  112. app.UseAuthorization();
  113. //配置Cors
  114. //设定特定ip允许跨域 CustomCorsPolicy是在ConfigureServices方法中配置的跨域策略名称
  115. app.UseCors("CustomCorsPolicy");
  116. //app.StartSchedulerJobs();
  117. //app.Use(next=> context => {
  118. // context.Request.EnableBuffering();
  119. // return next(context);
  120. // });
  121. app.UseEndpoints(endpoints =>
  122. {
  123. endpoints.MapControllers();
  124. //endpoints.MapHub<TokenSender>("/hubs/token");
  125. });
  126. }
  127. }
  128. }