using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Cksoft.Unity; using Cksoft.Unity.Log4NetConfig; using DllEapCommon.Filters; using Microsoft.AspNetCore.Authentication.JwtBearer; 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 Microsoft.IdentityModel.Tokens; using WebUpload.Filters; namespace WebUpload { 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 fea = new ControllerFeature(); //var assembly = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "DllEapBll.dll"); //var ufpAssembly = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "DllUfpBll.dll"); //var mesAssembly = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "DllDiodesMesBll.dll"); //m.ApplicationParts.Add(new AssemblyPart(assembly)); //m.ApplicationParts.Add(new AssemblyPart(ufpAssembly)); //m.ApplicationParts.Add(new AssemblyPart(mesAssembly)); //m.PopulateFeature(fea); //services.AddSingleton(fea.Controllers.Select(t => t.AsType()).ToArray()); }); // services.AddAdtSystem(Configuration); 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.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.UseCors("all"); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseCors(builder => builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); //app.UseHttpsRedirection(); //app.UseStaticFiles(); //app.UseCookiePolicy(); //app.UseMvc(routes => //{ // routes.MapRoute( // name: "default", // template: "{controller=Home}/{action=Index}/{id?}"); //}); } } }