using log4net; using log4net.Config; using log4net.Repository; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Connections; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Authorization; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.StaticFiles; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; using Newtonsoft.Json.Serialization; using Quartz; using Quartz.Impl; using Quartz.Spi; using Swashbuckle.AspNetCore.SwaggerUI; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using VueWebCoreApi.ApiGroup; using VueWebCoreApi.Extensions; using VueWebCoreApi.Quartz; using VueWebCoreApi.SignalR; using VueWebCoreApi.Tools; namespace VueWebCoreApi { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } //log4netÈÕÖ¾ public static ILoggerRepository repository { get; set; } //¿çÓòÉèÖà readonly string ganweiCosr = "AllowSpecificOrigins"; // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddNewtonsoftJson(op => { op.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(); op.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; // ·µ»ØÊý¾ÝÊ××Öĸ²»Ð¡Ð´£¬CamelCasePropertyNamesContractResolverÊÇСд op.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); //½â¾öÎĵµÖÐÊ××Öĸ±»Ð¡Ð´µÄÎÊÌâ services.AddControllers().AddJsonOptions(config => { config.JsonSerializerOptions.PropertyNamingPolicy = null; }); #region Swagger µÚ¶þÖÖ·½Ê½ services.AddSwaggerGen(c => { typeof(OpenApiGroup).GetEnumNames().ToList().ForEach(version => { c.SwaggerDoc(version, new OpenApiInfo() { Title = $"ÇÅϹ¤Ã³-MesApiÎĵµ:{version}", Version = version, Description = $"Panda.Sewerage : {version} ", }); }); string basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location); string xmlPath = Path.Combine(basePath, "VueWebCoreApi.xml"); c.IncludeXmlComments(xmlPath, true); }); #endregion // ÉèÖÃÅäÖà AppSetting.SetAppSetting(Configuration.GetSection("ConnectionStrings")); //log4netÅäÖà repository = LogManager.CreateRepository("AprilLog"); XmlConfigurator.Configure(repository, new FileInfo("Config/log4net.config")); BasicConfigurator.Configure(repository); services.AddControllersWithViews(); //Quartz services.AddHttpClient(); services.AddHttpContextAccessor(); services.AddSingleton(); services.AddTransient(); services.AddSingleton(); services.AddSingleton(); //¿çÓòÉèÖà services.AddCors(options => { options.AddPolicy( "cors", set => { set.SetIsOriginAllowed(origin => true).AllowAnyHeader().AllowAnyMethod().AllowCredentials(); }); }); //ÉèÖÃSignalR services.AddSignalR(); //ÉèÖÃsession,²¢ÉèÖó¬Ê±Ê±¼ä services.AddSession(o => { o.IdleTimeout = TimeSpan.FromSeconds(double.Parse(AppSetting.GetAppSetting("InProc"))); }); //ÉèÖÃCookies services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(); #region ÔÊÐí´óÎļþÉÏ´« //ÔÊÐíÉÏ´«´óÎļþ services.Configure(options => { options.MaxRequestBodySize = 1073741824;//´Ë´¦ÏÞÖÆ×î´ó1G }); //½â¾öÎļþÉÏ´«Multipart body length limit 134217728 exceeded. services.Configure(x => { x.ValueLengthLimit = int.MaxValue; x.MultipartBodyLengthLimit = 1073741824; //´Ë´¦ÏÞÖÆ×î´ó1G }); #endregion ÔÊÐí´óÎļþÉÏ´« } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime applicationLeftTime, ILoggerFactory loggerFactory) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } applicationLeftTime.ApplicationStarted.Register(() => { DataOperator.SetConnectionString1(Convert.ToString(AppSetting.GetAppSetting("DBServer"))); }); #region Swagger·Ö×éµÚ¶þÖÖ app.UseSwagger(); app.UseSwaggerUI(c => { //ɾ³ýSwaggerÉÏĬÈÏSchemas¼Ü¹¹ c.DefaultModelsExpandDepth(-1); //c.SwaggerEndpoint("/swagger/v1/swagger.json", "Api Document"); typeof(OpenApiGroup).GetEnumNames().ToList().ForEach(version => { c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"{version}"); }); }); #endregion app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); //Ìí¼Ó¾²Ì¬ÎļþÖмä¼þ app.UseDefaultFiles(); //Ìí¼ÓÊÚȨÎļþ app.UseStaticFiles(new StaticFileOptions() { ContentTypeProvider = new FileExtensionContentTypeProvider() { Mappings = { [".xls"] = "application/vnd.ms-excel", [".apk"] = "application/vnd.android.package-archive" } } }); app.UseStaticFiles(); //¿çÓò app.UseCors("cors"); //Óû§session·þÎñ app.UseSession(); //cookies app.UseCookiePolicy(); //HttpContext app.UseStaticHostEnviroment(); //Quartz app.UseQuartz(env).UseStaticHttpContext(); //ÅäÖÃExcelÏÂÔØ //app.UseStaticFiles( // new StaticFileOptions // { // //ÉèÖò»ÏÞÖÆcontent-type // ServeUnknownFileTypes = true // }); //app.UseStaticFiles(new StaticFileOptions() //{ // ContentTypeProvider = new FileExtensionContentTypeProvider() // { // Mappings = { [".exe"] = "application/octect-stream" } // } //}); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapHub("/ChatHub", options => { options.Transports = HttpTransportType.WebSockets | HttpTransportType.LongPolling; }); }); loggerFactory.AddLog4Net("Config/log4net.config"); } } }