using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Bowin.Common.Cache; using Bowin.Common.DES; using Bowin.Common.Filters; using Bowin.Common.Scheduler; using Bowin.Common.ServiceToken; using Bowin.Common.Utility; using YLShipBuildLandMap.Entity; using YLShipBuildLandMap.Entity.ViewModel; using YLShipBuildLandMap.Services.SystemSetting; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.StaticFiles; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Newtonsoft.Json.Serialization; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore.Infrastructure; using YLShipBuildLandMap.Entity.Extensions; using System.Reflection; using Bowin.Common.ServiceToken.ApiIdentity; namespace YLShipBuildLandMap.Web { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { ServiceConfiguration.RegistLocalServices(services); services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DataContext"), builder => { /*builder.UseRowNumberForPaging(); */ }) ).AddScoped(); services.AddServiceAssembly(Assembly.Load("YLShipBuildLandMap.Services"), Assembly.Load("YLShipBuildLandMap.IServices")); //services.BuildServiceProvider(); services.UseCache(); //配置缓存为3分钟 services.AddOptions().Configure(Configuration.GetSection(nameof(MemoryCacheEntryOptions))) .Configure(op => { //op.SlidingExpiration = TimeSpan.FromMinutes(3); op.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1); }); services.AddBowinAuthentication(x => HttpHelper.GetService().GetLoginUserById(x), x => HttpHelper.GetService().GetUserFunctionCodes(x)); services.AddQuartz(); services.AddControllers(configure => { configure.Filters.Add(typeof(CustomerExceptionFilter)); }) .AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local; options.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat; }); //services.AddSingleton(); services.UseBowinDes(); services.AddApiIdentity(); //允许一个或多个来源可以跨域 /*services.AddCors(options => { options.AddPolicy("CustomCorsPolicy", policy => { // 设定允许跨域的来源,有多个可以用','隔开 policy.SetIsOriginAllowed(x => true) // .WithOrigins(YLShipBuildLandMap.Web.Configuration.Current.CROSDomainList.ToArray()) .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); });*/ services.AddHttpClient(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); } app.UseDefaultFiles(new DefaultFilesOptions { DefaultFileNames = new List { "default.html" } }); var provider = new FileExtensionContentTypeProvider(); provider.Mappings[".properties"] = "application/octet-stream"; app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider }); YLShipBuildLandMapContext.SetServices(app.ApplicationServices); app.UseWebSockets(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); //配置Cors //设定特定ip允许跨域 CustomCorsPolicy是在ConfigureServices方法中配置的跨域策略名称 //app.UseCors("CustomCorsPolicy"); app.StartSchedulerJobs(); //app.Use(next=> context => { // context.Request.EnableBuffering(); // return next(context); // }); app.UseEndpoints(endpoints => { endpoints.MapControllers(); //endpoints.MapHub("/hubs/token"); }); } } }