using System.Collections.Generic; using System.IO; using System.Text.Encodings.Web; using System.Text.Unicode; using System.Threading.Tasks; using Edu.ThirdCore.Message; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Edu.WebApi { 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) { services.Configure(x => x.AllowSynchronousIO = true) .Configure(x => x.AllowSynchronousIO = true); services.AddControllers(); services.AddSingleton(); //注入http上下文 services.AddMvc().AddJsonOptions(options => { options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); options.JsonSerializerOptions.PropertyNamingPolicy = null; }); List corsArray = new List() { "http://192.168.20.214:8400", "http://localhost:8400", "http://localhost:8081", "http://localhost:8080", "http://localhost:8181", "http://localhost:8182", "http://localhost:8082", "http://127.0.0.1:50512", "http://127.0.0.1:20224", "http://127.0.0.1:28221", "http://www.test.com:8080", "http://www.test.com:8081", "http://yx.oytour.com", "http://mall.oytour.com", "http://testmall.oytour.com", "http://yx.oytour.com", "http://edu.oytour.com", "http://jiahe.oytour.com", "http://jiahe.oytour.com", "http://www.kookaku.com", "http://www.kookaku.cn", "http://kookaku.com", "https://mallapi.oytour.com", "https://mall.oytour.com" }; services.AddCors(options => options.AddPolicy("AllowCors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().WithOrigins(corsArray.ToArray()))); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime appLifetime) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); // 允许所有跨域,cors是在ConfigureServices方法中配置的跨域策略名称 app.UseCors("AllowCors"); app.UseAuthorization(); //启动信息发送 // Task.Run(() => MessageCore.Init()); Task.Run(() => MessageCore.Init()); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); appLifetime.ApplicationStarted.Register(() => { Timers.TimerJobj.RunTimer(); //网站启动完成执行 }); appLifetime.ApplicationStopped.Register(() => { Timers.TimerJobj.RunStop(); //网站停止完成执行 }); string filePath = Path.Combine(Directory.GetCurrentDirectory(), "upfile"); if (System.IO.Directory.Exists(filePath) == false)//如果不存在就创建file文件夹 { System.IO.Directory.CreateDirectory(filePath); } app.UseStaticFiles(new StaticFileOptions { FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), "upfile")), RequestPath = "/upfile" }); } } }