using Cksoft.Unity; using DllEapCommon.Filters; using DllEapFileUpload; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Linq; using System.Text; using WebApplet.Filters; using WebApplet.ServiceExtensions; namespace WebApplet { 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(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); Action filters = new Action(r => { // r.Filters.Add(typeof(WebApiResultMiddleware)); // 格式化 ADT系统 返回数据 r.Filters.Add(typeof(ExceptionFilterFor)); //r.Filters.Add(typeof(ButtonFilter)); r.Filters.Add(typeof(QueryStringFilter)); // 设置接收和返回值可以为XML格式 r.InputFormatters.Add(new XmlSerializerInputFormatter(new MvcOptions { })); r.OutputFormatters.Add(new XmlSerializerOutputFormatter()); }); services.AddMvc(filters).SetCompatibilityVersion(CompatibilityVersion.Version_2_1) .ConfigureApplicationPartManager(m =>{ }); var manager = new ApplicationPartManager(); manager.FeatureProviders.Add(new ControllerFeatureProvider()); var feature = new ControllerFeature(); manager.PopulateFeature(feature); services.AddSingleton(feature.Controllers.Select(t => t.AsType()).ToArray()); AppConfigurtaionServices.Instance = services.BuildServiceProvider(); services.AddCors(options => { options.AddPolicy("all", builder => { builder.AllowAnyOrigin() //允许任何来源的主机访问 .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials();//指定处理cookie }); }); services.AddNSwag(); services.AddSingleton(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory factory) { NLog.ILogger logger = NLog.LogManager.LoadConfiguration("NLog.config").GetCurrentClassLogger(); NLog.LogManager.Configuration.Variables["connectionString"] = Configuration["eap:ConnectionStrings"]; Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); //避免日志中的中文输出乱码 ILogger myloger = factory.AddLog4Net().CreateLogger(typeof(Startup)); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); } string errorinfo = ""; myloger.LogError($"启动服务成功:{errorinfo}"); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseStaticFiles(); app.UseCookiePolicy(); app.UseNSwag(); app.UseCors(builder => builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); if (Configuration["EapSocket"] != null && Convert.ToBoolean(Configuration["EapSocket"]) == true) { var EapScoket = app.ApplicationServices.GetRequiredService(); errorinfo = ""; EapScoket.Start(ref errorinfo); myloger.LogError(errorinfo); } //app.UseHttpsRedirection(); //app.UseStaticFiles(); //app.UseCookiePolicy(); //app.UseMvc(routes => //{ // routes.MapRoute( // name: "default", // template: "{controller=Home}/{action=Index}/{id?}"); //}); } } }